首页 > 代码库 > asp.net mvc使用log4gNetz
asp.net mvc使用log4gNetz
1. 下载安装log4gNet 2. 将 \bin\net\4.0\release\log4net.dll 复制到你的项目中 。 3. 将log4net.dll 添加引用到你的项目中。 4. 添加如下内容到 assemblyinfo.cs。 [assembly: log4net.Config.XmlConfigurator(ConfigFile = @"Config\Log4Net.config", Watch = true)] 5.在项目中创建一个新的log4net的配置文件Log4Net.config。
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <!--数据日志--> <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value=http://www.mamicode.com/"D:\wwwroot\admin_citssh_com\logfiles\Info\" /> <param name="AppendToFile" value=http://www.mamicode.com/"true" /> <param name="rollingStyle" value=http://www.mamicode.com/"Date" /> <param name="datePattern" value=http://www.mamicode.com/"yyyy-MM-dd.‘Info.log‘" /> <param name="staticLogFileName" value=http://www.mamicode.com/"false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value=http://www.mamicode.com/"记录时间:%date %n日志级别: %-5level %n类型:%logger %n描述:%message %newline%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value=http://www.mamicode.com/"INFO" /> <param name="LevelMax" value=http://www.mamicode.com/"INFO" /> </filter> </appender> <!--错误日志--> <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value=http://www.mamicode.com/"D:\wwwroot\admin_citssh_com\logfiles\Error\" /> <param name="AppendToFile" value=http://www.mamicode.com/"true" /> <param name="rollingStyle" value=http://www.mamicode.com/"Date" /> <param name="datePattern" value=http://www.mamicode.com/"yyyy-MM-dd.‘error.log‘" /> <param name="staticLogFileName" value=http://www.mamicode.com/"false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value=http://www.mamicode.com/"记录时间:%date %n日志级别: %-5level %n类型:%logger %n描述:%message %newline%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value=http://www.mamicode.com/"ERROR" /> <param name="LevelMax" value=http://www.mamicode.com/"ERROR" /> </filter> </appender> <!--调试日志--> <appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" value=http://www.mamicode.com/"D:\wwwroot\admin_citssh_com\logfiles\Debug\" /> <param name="AppendToFile" value=http://www.mamicode.com/"true" /> <param name="rollingStyle" value=http://www.mamicode.com/"Date" /> <param name="datePattern" value=http://www.mamicode.com/"yyyy-MM-dd.‘debug.log‘" /> <param name="staticLogFileName" value=http://www.mamicode.com/"false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value=http://www.mamicode.com/"记录时间:%date %n日志级别: %-5level %n类型:%logger %n描述:%message %newline%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value=http://www.mamicode.com/"DEBUG" /> <param name="LevelMax" value=http://www.mamicode.com/"DEBUG" /> </filter> </appender> <root> <level value=http://www.mamicode.com/"DEBUG" /> <!--文件形式记录日志--> <appender-ref ref="ErrorRollingFileAppender" /> <appender-ref ref="DebugRollingFileAppender" /> <appender-ref ref="InfoRollingFileAppender" /> </root> </log4net> </configuration>
6.在全局配置中添加如下代码: log4net.Config.XmlConfigurator.Configure();
7.添加log操作类:
public static class Log { public static void Debug(object message) { LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message); } public static void Debug(object message, Exception ex) { LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message, ex); } public static void Error(object message) { LogManager.GetLogger(GetCurrentMethodFullName()).Error(message); } public static void Error(object message, Exception exception) { LogManager.GetLogger(GetCurrentMethodFullName()).Error(message, exception); } private static string GetCurrentMethodFullName() { StackFrame frame; string str; string str1; bool flag; try { int num = 2; StackTrace stackTrace = new StackTrace(); int length = stackTrace.GetFrames().Length; do { int num1 = num; num = num1 + 1; frame = stackTrace.GetFrame(num1); str = frame.GetMethod().DeclaringType.ToString(); flag = (!str.EndsWith("Exception") ? false : num < length); } while (flag); string name = frame.GetMethod().Name; str1 = string.Concat(str, ".", name); } catch { str1 = null; } return str1; } public static void Info(object message) { LogManager.GetLogger(GetCurrentMethodFullName()).Info(message); } public static void Info(object message, Exception ex) { LogManager.GetLogger(GetCurrentMethodFullName()).Info(message, ex); } public static void Warn(object message) { LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message); } public static void Warn(object message, Exception ex) { LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message, ex); } }
asp.net mvc使用log4gNetz
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。