C#通过log4net进行异常记录
C#中异常的记录也有一个模板,就是log4net。多的就不说了直接看怎么用的吧。 1、引用log4net.dll。
2、实现log4net的方法。
public class LogImplement { private ILog logger; public LogImplement(ILog log) { this.logger = log; } public void Debug(object message) { this.logger.Debug(message); } public void Debug(object message, Exception e) { this.logger.Debug(message, e); } public void Warming(object message) { this.logger.Warn(message); } public void Warming(object message,Exception e) { this.logger.Warn(message, e); } public void Error(object message) { this.logger.Error(message); } public void Error(object message, Exception e) { this.logger.Error(message, e); } public void Info(object message) { this.logger.Info(message); } public void Info(object message, Exception e) { this.logger.Info(message, e); } } public class LogFactory { static LogFactory() { FileInfo configFile = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"Log.config"); log4net.Config.XmlConfigurator.Configure(configFile); } public static LogImplement GetLogger(Type type) { return new LogImplement(LogManager.GetLogger(type)); } public static LogImplement GetLogger(string str) { return new LogImplement(LogManager.GetLogger(str)); } }
3、配置文件
<configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net-net-2.0"/> </configSections> <log4net> <root> <appender-ref ref="ErrorLogFileAppender"/> <appender-ref ref="WarningFileAppender"/> <appender-ref ref="DebugLogFileAppender"/> </root> <!--记录warning--> <appender name="WarningFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log/Warn.log"/> <appendToFile value="true"/> <maxSizeRollBackups value="10"/> <maximumFileSize value="1MB"/> <rollingStyle value="Size"/> <staticLogFileName value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%date] [%-5level] [%logger] - %message%newline"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="WARN"/> <param name="LevelMax" value="WARN"/> </filter> </appender> <!--记录错误--> <appender name="ErrorLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log/Error.log"/> <appendToFile value="true"/> <maxSizeRollBackups value="10"/> <maximumFileSize value="1MB"/> <rollingStyle value="Size"/> <staticLogFileName value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="[%date] [%-5level] [%logger] - %message%newline"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="ERROR"/> <param name="LevelMax" value="ERROR"/> </filter> </appender> <!--记录调试日志--> <appender name="DebugLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log/debug.log"/> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="10"/> <param name="MaximumFileSize" value="1MB"/> <param name="StaticLogFileName" value="true"/> <param name="RollingStyle" value="Size"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="[%p] [%d{MM/dd/yyyy HH:mm:ss,fff}] [%t] [%c]- %m%n"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="DEBUG"/> <param name="LevelMax" value="DEBUG"/> </filter> </appender> <!--调试模式,输出日志到控制台--> <appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR"/> <foreColor value="Red, HighIntensity"/> <!--<backColor value="Green" />--> </mapping> <mapping> <level value="DEBUG"/> <foreColor value="Yellow, HighIntensity"/> <backColor value="Green"/> </mapping> <layout type="log4net.Layout.PatternLayout"> <footer value="--------------------------------------------------------------------"/> <param name="ConversionPattern" value="[%p] [%d{MM/dd/yyyy HH:mm:ss,fff}] [%t] [%c]- %m%n"/> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="DEBUG"/> <param name="LevelMax" value="ERROR"/> </filter> </appender> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
其中, 表示异常文件超过1M大小就自动新增文件;表示异常文件的路径;表示要支持的.net版本。至于其他属性在使用中自己可以慢慢体会。
4、记录异常
public class RecordLog { public static void RecordException(Exception e) { LogImplement log = LogFactory.GetLogger(typeof(RecordLog)); log.Error(e.Message + e.StackTrace); } }
5、调用
static void Main(string[] args) { try { string test = "123tr"; int i = int.Parse(test);//制造一个异常 } catch(Exception e) { RecordLog.RecordException(e); } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/11117.html