1:创建log4net 配置文件log4net.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <!-- 将日志写入到本地txt文件中 --> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!--日志路径--> <param name= "File" value= "logs"/> <!--是否是向文件中追加日志--> <param name= "AppendToFile" value= "true"/> <!--log保留天数--> <param name= "MaxSizeRollBackups" value= "10"/> <!--日志文件名是否是固定不变的--> <param name= "StaticLogFileName" value= "false"/> <!--日志文件名格式为:2008-08-31.log--> <param name= "DatePattern" value= "yyyy-MM-dd HH'.log'"/> <!--日志根据日期滚动--> <param name= "RollingStyle" value= "Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%n%d 耗时%r毫秒 进程ID:%t 行号:%L 当前日志对象名称:%c 日志当前级别:%p 输出语句所在文件名:%F 日志消息:%m" /> </layout> </appender> <!-- 控制台前台显示日志 --> <!--<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="Red" /> </mapping> <mapping> <level value="WARN" /> <foreColor value="Yellow" /> </mapping> <mapping> <level value="INFO" /> <foreColor value="White" /> </mapping> <mapping> <level value="DEBUG" /> <foreColor value="Blue" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n%d 程序运行时间:%r毫秒 进程ID:%t 行号:%L 当前日志对象名称:%c 日志当前级别:%p 输出语句所在文件名:%F 日志消息:%m" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Info" /> <param name="LevelMax" value="Fatal" /> </filter> </appender>--> <!-- 将日志写入到SQLServer中 --> <!--<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="1" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="data source=.\MSSQL2008R2;initial catalog=MeiTuan;integrated security=false;persist security info=True;User ID=sa;Password=111111" /> <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" /> <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="50" /> <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> <parameter> <parameterName value="@exception" /> <dbType value="String" /> <size value="2000" /> <layout type="log4net.Layout.ExceptionLayout" /> </parameter> </appender>--> <root> <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) --> <level value="all" /> <!--<appender-ref ref="ColoredConsoleAppender"/>--> <appender-ref ref="RollingLogFileAppender"/> <!--<appender-ref ref="AdoNetAppender"/>--> </root> </log4net> </configuration>
2:公共日志类:
public class Log4netHelper { private static readonly log4net.ILog logger = log4net.LogManager.GetLogger("Logger"); //这里的 Logger 和 log4net.config 里的名字要一样 public static void Info(string message) { if (logger.IsInfoEnabled) { logger.Info(message); } } public static void Info(string message, Exception ex) { if (logger.IsInfoEnabled) { logger.Info(message, ex); } } public static void Debug(string message) { if (logger.IsErrorEnabled) { logger.Debug(message); } } public static void Debug(string message, Exception ex) { if (logger.IsErrorEnabled) { logger.Warn(message, ex); } } public static void Warn(string message) { if (logger.IsErrorEnabled) { logger.Warn(message); } } public static void Warn(string message, Exception ex) { if (logger.IsErrorEnabled) { logger.Warn(message, ex); } } public static void Error(string message) { if (logger.IsErrorEnabled) { logger.Error(message); } } public static void Error(string message, Exception ex) { if (logger.IsErrorEnabled) { logger.Error(message, ex); } } public static void Fatal(string message) { if (logger.IsErrorEnabled) { logger.Fatal(message); } } public static void Fatal(string message, Exception ex) { if (logger.IsErrorEnabled) { logger.Fatal(message, ex); } } }
3:Assemblyinfor 文件中添加如下代码
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
注:
1:log4net.config 文件必须放在客户端项目中,web项目放在web页面层
2;logger.IsErrorEnabled,IsInfoEnabled,IsDebugEnabled,IsWarnEnabled,IsFatalEnabled 等属性要起作用,必须将log4net.config 文件的设置为始终复制或者如果较新则复制
标签:log4net,message,static,ex,使用,logger,public From: https://www.cnblogs.com/leon1128/p/17819656.html