首先添加加log4net的dll,推荐用nuget....
贴上配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4Net" /> </configSections> <!--日志的配置开始--> <log4net debug="true"> <root> <level value="ALL" /> <appender-ref ref="SysAppender" /> <appender-ref ref="AdoNetAppender" /> </root> <logger name="WebLogger"> <!--配置日志的级别,低于此级别的就不写到日志里面去--> <level value="DEBUG" /> </logger> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net"> <param name="File" value="App_Data/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".htm"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" /> <!--<conversionPattern value="%newline %n记录时间:%date %newline %n 线程ID:[%thread] %n日志级别: %-5level %n跟踪描述:%message%newline %n"/>--> </layout> </appender> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="1" /> <connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> <connectionString value="server=localhost;port=3306;database=jianzheng;uid=root;pwd=svse;" /> <commandText value="INSERT INTO `TestSysLog` (`Date`, `Thread`, `Level`, `Logger`, `Method`, `Location`,`Message`, `Exception`) VALUES (@Date, @Thread, @Level, @Logger, @Method,@Location,@Message, @Exception)" /> <parameter> <parameterName value="@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="@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="@Method" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%method" /> </layout> </parameter> <parameter> <parameterName value="@Location" /> <dbType value="String" /> <size value="1023" /> <layout type="log4net.Layout.PatternLayout" > <conversionPattern value="%location" /> </layout> </parameter> <parameter> <parameterName value="@Message" /> <dbType value="String" /> <size value="4000" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> <parameter> <parameterName value="@Exception" /> <dbType value="String" /> <size value="8000" /> <layout type="log4net.Layout.ExceptionLayout" /> </parameter> </appender> </log4net> <!--日志的配置结束--> </configuration>本demo里是新建的一个xml文件。在网站根目录新建如图:把xml配置内容copy进去,在Global.asax的Application_Start()中加入下面的一句 //读取日志 如果使用log4net,应用程序一开始的时候,都要进行初始化配置 log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("config\\log4net.config"));在AssemblyInfo.cs里加入[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Config\\log4net.config", Watch = false)]
AssemblyInfo.cs的位置在如图:
然后添加一个过滤器
public class MyExceptionFileAttribute : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { base.OnException(filterContext); //处理错误消息,将其跳转到一个页面 LogHelper.WriteLog(filterContext.Exception.ToString()); //页面跳转到错误页面 filterContext.HttpContext.Response.Redirect("/Error.html"); } }
还要建一个工具类,用来写日志。
public class LogHelper { public static void WriteLog(string txt) { ILog log = LogManager.GetLogger("log4netlogger"); log.Error(txt); } }
最后还要在
FilterConfig.cs中加入全局过滤器 //注册错误过滤器 public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); //注册错误过滤器 filters.Add(new MyExceptionFileAttribute()); }默认把错误日志存在app_data中,为htm文件。存到mysql数据库中了数据库结构如图:
时间: 2024-10-19 11:49:21