C# web程序,winform程序,控制台程序配置log4net,使用log4net
2021/4/26 20:27:29
本文主要是介绍C# web程序,winform程序,控制台程序配置log4net,使用log4net,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
控制台程序配置log4net, 使用log4net, C# web程序配置log4net, C# winform程序配置log4net
第一添加log4net.config,这里配置包括信息提示写入,错误信息写入,控制台消息展示
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> configSections> <log4net> 信息日志配置--> <appender name="infoAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Logs\Info\info.log" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="PreserveLogFileNameExtension" value="true" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> layout> appender> 错误日志配置--> <appender name="errorAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Logs\Error\Err.log" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="PreserveLogFileNameExtension" value="true" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> layout> appender> 控制台前台显示日志 --> <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="Red, HighIntensity" /> mapping> <mapping> <level value="Info" /> <foreColor value="Green" /> mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" /> layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Info" /> <param name="LevelMax" value="Fatal" /> filter> appender> <root> 配置日志的级别,低于此级别的就不写到日志里面去 None>Fatal>ERROR>WARN>DEBUG>INFO>ALL--> <level value="ALL" /> <appender-ref ref="infoAppender" /> <appender-ref ref="ColoredConsoleAppender" /> root> <logger name="loginfo"> <level value="INFO" /> <appender-ref ref="infoAppender" /> logger> <logger name="logerror"> <level value="ERROR" /> <appender-ref ref="errorAppender" /> logger> log4net>configuration>
第二将log4net.config注入到log4net中
//Winform窗体程序或者控制台程序,加载log4net配置文件,以下代码是写在Program类的Main方法中class Program { static void Main(string[] args) { //加载log4net配置文件 var filepath = AppDomain.CurrentDomain.BaseDirectory + "log4net.config"; log4net.Config.XmlConfigurator.Configure(new FileInfo(filepath)); } }
//Web程序,加载Log4net配置文件 ,以下代码是写在Global.cs类中,例如protected void Application_Start(object sender, EventArgs e) { var configPath = HttpContext.Current.Server.MapPath("~/log4net.config"); log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configPath)); }
第三使用log4net SDK,去NUGET搜索log4net并加载到需要的项目中
/// /// 打印日志 /// public static class LogHelper { /// /// 打印提示 /// /// public static void Info(string txt) { ILog log = log4net.LogManager.GetLogger("loginfo"); log.Info(txt); } /// /// 打印提示 /// /// public static void Info(string txt, Type type) { ILog log = log4net.LogManager.GetLogger(type); log.Info(txt); } /// /// 打印错误 /// /// public static void Error(string msg) { ILog log = log4net.LogManager.GetLogger("logerror"); log.Error(msg); } /// /// 打印错误 /// /// public static void Error(string msg, Exception ex) { ILog log = log4net.LogManager.GetLogger("logerror"); log.Error(msg, ex); } }
代码使用例子
protected void Page_Load(object sender, EventArgs e) { try { string a = "FF"; int b = Convert.ToInt32(a); } catch (Exception ex){ LogHelper.Error(ex.Message.ToString(), ex); } }
如果您觉得有用就直接拿去不用谢,axinno1
这篇关于C# web程序,winform程序,控制台程序配置log4net,使用log4net的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具