首页 > 代码库 > 如何配置log4Net
如何配置log4Net
之前曾经用过几次,但是每次都是用完就忘了,下次再用的时候要baidu半天,这次弄通之后直接记下来。
步骤如下。
1. 安装log4Net,直接用NuGet, Install-Package log4Net
2. 把Log4Net.config这个配置文件加到工程里面,切记要把属性改成"Copy Always"。文件内容如下。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net> <root> <level value=http://www.mamicode.com/"DEBUG" /> <appender-ref ref="RollingLogFileAppender" /> </root> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value=http://www.mamicode.com/"Log\" /> <appendToFile value=http://www.mamicode.com/"true" /> <rollingStyle value=http://www.mamicode.com/"Composite" /> <maxSizeRollBackups value=http://www.mamicode.com/"100" /> <maximumFileSize value=http://www.mamicode.com/"2MB" /> <staticLogFileName value=http://www.mamicode.com/"false" /> <param name="DatePattern" value=http://www.mamicode.com/"yyyy-MM-dd".log""/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value=http://www.mamicode.com/"[%date] %thread %-5level %message%newline" /> </layout> </appender> </log4net> </configuration>
3. 在工程里面加一个类,内容如下。
using System; namespace AutoFlashingTool { class Logger { log4net.ILog _log = null; public Logger() { _log= log4net.LogManager.GetLogger("default"); } public void WriteDebug(string msg) { _log.Debug(msg); } public void WriteInfo(string msg) { _log.Info(msg); } public void WriteWarning(string msg) { _log.Warn(msg); } public void WriteError(string msg, Exception ex) { _log.Error(msg, ex); } } }
4. 在AssemblyInfo.cs文件里面加上一行,告诉应用程序,log4Net的相关配置在log4Net.config这个文件里面
using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Windows.Media; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("AutoFlashingTool")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("BOSCH")] [assembly: AssemblyProduct("AutoFlashingTool")] [assembly: AssemblyCopyright("Copyright ? BOSCH 2017")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("d9ea3b80-f51a-4b54-8173-a8757d35429e")] // required to support per-monitor DPI awareness in Windows 8.1+ // see also https://mui.codeplex.com/wikipage?title=Per-monitor%20DPI%20awareness [assembly: DisableDpiAwareness] // Version information for an assembly consists of the following four values: // // Major Version // Minor Version // Build Number // Revision // // You can specify all the values or you can default the Build and Revision Numbers // by using the ‘*‘ as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
5. 在代码里实例化Logger这个类,就可以调用方法记录Log了。
如何配置log4Net
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。