在 C# Web API + html/JS/JQuery 项目中,我目前正在使用 log4net 日志库获取信息(服务器端)。
现在,我还需要记录有关客户端事件的信息。
我找到了库 JSNLog.Log4Net 版本:2.20.1,它似乎可以工作。
我的问题是我需要自定义日志,就像我对 log4net 所做的那样。
在 log4net.config 文件之后:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="0" />
<reconnectOnError value="true"/>
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString .... />
<commandText value="INSERT INTO [INT].[Log] ([Appl],[Level],[IP],[HttpMethod],[API],[Request],[DataInput],[Response],[DataOutput],[UserName],[Message],[Exception],[ExecutedTime])
VALUES (@APPL, @log_level, @IP, @logger, @API, CASE WHEN (@Request='(null)') THEN null ELSE @Request END, @DataInput, CASE WHEN (@Response='(null)') THEN null ELSE @Response END, @DataOutput, @UserName, @Message, @Exception, @ExecutedTime)" />
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="16" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p" />
</layout>
</parameter>
<parameter>
<parameterName value="@APPL" />
<dbType value="String" />
<size value="16" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{APPL}" />
</layout>
</parameter>
<parameter>
<parameterName value="@IP"/>
<dbType value="String" />
<size value="128" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{IP}" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%c" />
</layout>
</parameter>
<parameter>
<parameterName value="@API"/>
<dbType value="String" />
<size value="128" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{API}" />
</layout>
</parameter>
<parameter>
<parameterName value="@Request" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Request}" />
</layout>
</parameter>
<parameter>
<parameterName value="@DataInput" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{DataInput}" />
</layout>
</parameter>
<parameter>
<parameterName value="@Response" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Response}" />
</layout>
</parameter>
<parameter>
<parameterName value="@DataOutput" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{DataOutput}" />
</layout>
</parameter>
<parameter>
<parameterName value="@Message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
</parameter>
<parameter>
<parameterName value="@Exception" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
<parameter>
<parameterName value="@UserName"/>
<dbType value="String" />
<size value="128" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{UserName}" />
</layout>
</parameter>
<parameter>
<parameterName value="@ExecutedTime"/>
<dbType value="Int32" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="ExecutedTime" />
</layout>
</parameter>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
<bufferSize value="0" />
<file value="Log\SC_Log.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value=".yyyyMMdd.lo\\g" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - (%file:%line) - %message%newline" />
</layout>
</appender>
<!--<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p [%x] - %m%n" />
</layout>
</appender>-->
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<!--<appender-ref ref="ConsoleAppender" />-->
<appender-ref ref="ADONetAppender" />
</root>
</log4net>
</configuration>
以下是我记录的示例:
如您所见,黄线(由 JSNLog.log4net 生成的那条)缺少很多信息。
我该如何添加它们?
再见
戴维德
PS .: 如有必要,我还可以更改用于客户端日志记录的库。