C# 日志框架的添加
.NET中 记录日志的比较好的主要是Log4Net和Enterprise Library的Logging
复杂一点的还可以实现自动化Log日志 教程
首先是第二种方式
1.需要添加以下几个DLL 下载
- Microsoft.Practices.EnterpriseLibrary.Logging.dll
- Microsoft.Practices.ObjectBuilder2.dll
- Microsoft.Practices.EnterpriseLibrary.Common.dll
2.Web.config 的configuration节点中添加以下内容
<configSections> <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </configSections> <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"> <listeners> <add fileName="Log\Service.log" footer="----------------------------------------" formatter="Text Formatter" rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="10240" timeStampPattern="yyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Rolling Flat File Trace Listener" /> </listeners> <formatters> <add template="时间: {timestamp} 类型: {severity} 标题: {title} 内容: {message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter" /> </formatters> <categorySources> <add switchValue="All" name="Debug" /> <add switchValue="All" name="General"> <listeners> <add name="Rolling Flat File Trace Listener" /> </listeners> </add> </categorySources> <specialSources> <allEvents switchValue="All" name="All Events" /> <notProcessed switchValue="All" name="Unprocessed Category" /> <errors switchValue="All" name="Logging Errors & Warnings" /> </specialSources> </loggingConfiguration>
注意:configSections节点一定要在loggingConfiguration节点的上面
3.调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Reflection; using System.Configuration; using Microsoft.Practices.EnterpriseLibrary.Logging; using System.Collections; public sealed class LogWriter { public LogWriter() { } /// <summary> /// 输出错误日志 /// </summary> /// <param name="exception"></param> public static void WriteError(Exception exception, string title) { LogEntry logEntity = new LogEntry(); logEntity.Title = title; logEntity.TimeStamp = DateTime.Now; logEntity.Message = exception.ToString(); logEntity.Severity = TraceEventType.Error; Logger.Write(logEntity); } }