2015年7月31日

Log4Net 學習紀錄

1.NuGet install log4net
2.Cerate file log4net.config
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
</configuration>
<configuration>
  <!--logger: 記錄器,可自訂設定屬性(可多個)-->
    <!--appender-ref: 指定參考的Appender設定-->
    <logger name="Rolling">
      <appender-ref ref="RollingFile" />
    </logger>
    <logger name="DB">
      <appender-ref ref="LogDatabase" />
    </logger>
</configuration>
<configuration>
  <!--root logger: 根記錄器,當其他記錄器執行完後最後會執行根記錄器(唯一)-->
    <!--level: 記錄級別設定-->
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingFile" />
    </root>
</configuration>
For File
<configuration>
  <!--appender: 內容為輸出格式的設定-->
    <!--type: 輸出類型,例如 ConsoleAppender 為主控台輸出、RollingFileAppender 為文字檔輸出-->
    <!--file: 輸出檔案的路徑位置-->
    <!--layout: 記錄的格式設定-->
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="C:\Log\PlayLog.txt" />
      <appendToFile value="true" />
      <maximumFileSize value="1000KB" />
      <maxSizeRollBackups value="2" />
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[Activity]%newlineActivityId: %property{GUID}%newlineTime: %date{yyyy-MM-dd HH:mm:ss}%newlineLevel: %level%newline%newline[System]%newlineSystem: %property{System}%newline%newline[IP]%newlineServerIP: %property{ServerIP}%newlineClientIP: %property{ClientIP}%newline%newline[Message]%newlineDescription: %property{Description}%newlineException: %property{Exception}%newline%newline[Properties]%newline%message%newline%newline" />
      </layout>
    </appender>
</configuration>
For DB
<configuration>
  <appender name="LogDatabase" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="100" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="Data Source={Server};Initial Catalog={DBName};User ID={UserID};Password={Password}" />
      <commandText value="INSERT INTO [dbo].[Message] (MessageId, Message) VALUES (@MessageId, @Message)" />
      <parameter>
        <parameterName value="@MessageId" />
        <dbType value="Int32" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{MessageId}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@Message" />
        <dbType value="String" />
        <size value="100" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{Message}" />
        </layout>
      </parameter>
    </appender>
</configuration>
3.Add Watch in Global.asax Application_Start()
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(Server.MapPath("~/log4net.config")));


儲存GUID
log4net.ThreadContext.Properties["GUID"] = guidValue;

<parameter>
    <parameterName value="@LogId" />
    <dbType value="Guid" />
    <layout type="log4net.Layout.RawPropertyLayout">
        <key value="GUID" />
    </layout>
</parameter>

Trace log4net debug message by modify web.config
<configuration>
  <appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>
</configuration>
<configuration>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="textWriterTraceListener"
             type="System.Diagnostics.TextWriterTraceListener"
             initializeData="C:\Log\log4net.txt"
             />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>