主要是設定檔
<log4net> <!--檔案日誌輸出--> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender, log4net"> <param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value="logs/" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyy-MM-dd.'log'" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="10" /> <param name="MaximumFileSize" value="5MB" /> <param name="RollingStyle" value="Date" /> <!--Size ==- Date--> <!--每條日誌末尾的文字說明--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="記錄時間:%date 線程ID:[%thread] 記錄層級:%-5level 出錯類:%logger property:[%property{NDC}] - 錯誤描述:%message%newline"/> <!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />--> </layout> </appender> <appender name="AdoNetAppender_PGSQL" type="log4net.Appender.AdoNetAppender, log4net"> <bufferSize value="1" /> --緩衝大小,日誌達到多少記錄然後寫入資料庫,這裡設定為1,是為了看效果 <connectionType value="Npgsql.NpgsqlConnection, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"/>--這個是postgresql需要的用戶端的控制項。寫資料庫使用 <connectionString value="Server=localhost;Database=GridGIS;Userid=postgres;Password=postgre;" /> <commandText value="INSERT INTO log_msg (log_date,thread,logger,message,log_level) VALUES (:log_date,:thread,:logger,:message,:log_level)" /> <parameter> <parameterName value="log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout"/> </parameter> <parameter> <parameterName value="thread" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%thread" /> </layout> </parameter> <parameter> <parameterName value="log_level" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level" /> </layout> </parameter> <parameter> <parameterName value="logger" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%logger" /> </layout> </parameter> <parameter> <parameterName value="message" /> <dbType value="String" /> <size value="4000" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> </appender> <root> <!--設定預設日誌器--> <level value="DEBUG" /> <appender-ref ref="AdoNetAppender_PGSQL" /> </root>
</log4net>
在具體項目的 AssemblyInfo.cs 中(不要在封裝的類庫中framwork),加入
[assembly: log4net.Config.XmlConfigurator( Watch=true)]
此檔案基本可以使用,如果不行就下載源碼調試吧。一個個參數驗證。