之前曾经用过几次,但是每次都是用完就忘了,下次再用的时候要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&quot;.log&quot;"/>
<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了。

最新文章

  1. Android开发学习之路-Android6.0运行时权限
  2. C++分类
  3. C#堆栈和托管堆
  4. AS 2.0新功能 Instant Run
  5. Subway POJ 2502
  6. Mysql 视图笔记
  7. Google搜索的配置方法
  8. grub2详解(翻译和整理官方手册)
  9. 解决mariadb grant ERROR 1045 (28000): Access denied for user
  10. [BZOJ1031] [JSOI2007] 字符加密Cipher (后缀数组)
  11. [PHP]实体类基类和序列化__sleep问题
  12. quratz数据存储
  13. 使用speex动态链接库过程中遇到问题及解决方法
  14. linq操作符:转换操作符
  15. [LeetCode] 181. Employees Earning More Than Their Managers_Easy tag: SQL
  16. 10条Linux 命令了解服务器当前性能
  17. Android 内存泄露总结(附内存检测工具)
  18. Nginx的反向代理和负载均衡
  19. 洛谷p1208 水题贪心 思想入门
  20. 【转】Gvim配置(Windows and Linux)for C++

热门文章

  1. TypeScript 中函数的理解?与 JavaScript 函数的区别?
  2. Python习题集(九)
  3. jvm学习笔记:类加载过程
  4. vue+element+echarts柱状图+列表
  5. vue页面跳转以及传参和取参
  6. 【第十八篇】- Maven Eclipse之Spring Cloud直播商城 b2b2c电子商务技术总结
  7. Ansible快速实战指南----多机自动化执行命令、部署神器
  8. PHP中的MySQLi扩展学习(二)mysqli类的一些少见的属性方法
  9. php获取纳秒方法
  10. dedecms织梦调用指定文章id