如何配置log4Net
2024-09-05 03:04:34
之前曾经用过几次,但是每次都是用完就忘了,下次再用的时候要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="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Log\" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="2MB" />
<staticLogFileName value="false" />
<param name="DatePattern" value="yyyy-MM-dd".log""/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%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了。
最新文章
- Android开发学习之路-Android6.0运行时权限
- C++分类
- C#堆栈和托管堆
- AS 2.0新功能 Instant Run
- Subway POJ 2502
- Mysql 视图笔记
- Google搜索的配置方法
- grub2详解(翻译和整理官方手册)
- 解决mariadb grant ERROR 1045 (28000): Access denied for user
- [BZOJ1031] [JSOI2007] 字符加密Cipher (后缀数组)
- [PHP]实体类基类和序列化__sleep问题
- quratz数据存储
- 使用speex动态链接库过程中遇到问题及解决方法
- linq操作符:转换操作符
- [LeetCode] 181. Employees Earning More Than Their Managers_Easy tag: SQL
- 10条Linux 命令了解服务器当前性能
- Android 内存泄露总结(附内存检测工具)
- Nginx的反向代理和负载均衡
- 洛谷p1208 水题贪心 思想入门
- 【转】Gvim配置(Windows and Linux)for C++