.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节点一定要在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() { } ////// 输出错误日志 /// /// 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); }}