JSNLog.Log4Net自定义日志

时间:2016-11-11 11:51:16

标签: javascript c# log4net jsnlog

在项目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>

下面是我记录的一个例子:

enter image description here

如您所见,黄线(JSNLog.log4net生成的黄线)缺少信息。

如何添加它们?

再见

的Davide

P.S .:如有必要,我还可以更改库以进行客户端日志记录。

1 个答案:

答案 0 :(得分:0)

缺少的条目是%property{X}个,客户端库无法访问,因为它们保存在服务器的内存中。如果要从客户端记录它们,则需要将属性值传递给它。