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

如何配置log4Net的更多相关文章

  1. 单元测试中如何配置log4net

    按道理来说,单元测试中基本没有对于日志的需求,这是由于单元测试的定位来决定的. 因为单元测试的思想就是针对的都是小段代码的测试,逻辑明确,如果测试运行不通过,简单调试一下,就能很容易地排查问题.但是单 ...

  2. log4net保存到数据库系列四、完整代码配置log4net

    园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志一.WebConfig中配置log4net 一.WebConfig中配置log4ne ...

  3. log4net保存到数据库系列三、代码中xml配置log4net

    园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志一.WebConfig中配置log4net 一.WebConfig中配置log4ne ...

  4. ASP.NET/MVC 配置log4net启用写错误日志功能

    <?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访 ...

  5. C#在window服务配置Log4Net.dll

    1.使用背景: C#window服务下添加一个日志记录程序集(Log4Net.dll) 2.添加和使用步骤如下: 一.下载并引入Log4Net.dll程序集到项目中 下载地址:http://loggi ...

  6. ASP.NET 配置log4net启用写错误日志功能

    http://www.cnblogs.com/yeminglong/archive/2013/05/21/3091192.html 首先我们到apche的官网下载log4net的项目编译得到log4n ...

  7. C# 在项目中配置Log4net

    我们介绍一下在项目中配置log4net,是Apache基金会旗下的. 无论在什么环境中,配置log4net的逻辑都一样. 1)文件配置 首先在项目加载文件中,配置log4net加载项. 在Web项目中 ...

  8. log4net保存到数据库系列二:独立配置文件中配置log4net

    园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志一.WebConfig中配置log4net 一.WebConfig中配置log4ne ...

  9. log4net保存到数据库系列一:WebConfig中配置log4net

    园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志 一.WebConfig中配置log4net 二.独立配置文件中配置log4net ...

  10. VS2012 C#使用/配置Log4Net

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精   本节探讨如何在VS2012中使用Log4Net 1. ...

随机推荐

  1. Mybatis(四)——

    test https://www.cnblogs.com/chiaki/p/14529418.html

  2. Spring Cloud Eureka 实践(一)

    Spring Cloud Eureka是Spring Cloud Netflix微服务套件中的一部分,主要在Spring Cloud架构中提供服务注册发现的功能.那么是不是可以尝试在本地搭一个单例Eu ...

  3. C# AutoMaper使用自定义主键

    有时候实际业务中主键不一定叫Id,比如示例数据库Northwind中的RegionID,TerritoryID等,本示例用Abp框架并以Northwind数据库Region表为数据依据 一.在Core ...

  4. 获取office版本

    /// <summary>         /// office版本         /// </summary>         public enum OfficeVers ...

  5. C# 动态构建表达式树(一)—— 构建 Where 的 Lambda 表达式

    C# 动态构建表达式树(一)-- 构建 Where 的 Lambda 表达式 前言 记得之前同事在做筛选功能的时候提出过一个问题:如果用户传入的条件数量不确定,条件的内容也不确定(大于.小于和等于), ...

  6. 【PHP数据结构】线性表?顺序表?链表?别再傻傻分不清楚

    遵从所有教材以及各类数据结构相关的书书籍,我们先从线性表开始入门.今天这篇文章更偏概念,是关于有线性表的一个知识点的汇总. 上文说过,物理结构是用于确定数据以何种方式存储的.其他的数据结构(树.图). ...

  7. 解决dede编辑器不能保存word文档样式问题

    ckeditor在dede里不能保存样式,试过多种解决办法都还是没有解决.最终将编辑器换成FCK得到解决. 第一步:下载FCK编辑器 下载地址: 链接: http://pan.baidu.com/s/ ...

  8. DEDECMS首页循环调用一级栏目和二级栏目的实现方法

    调用方法: {dede:channelartlist typeid='2'} <li class="First"><a href="{dede:fiel ...

  9. Linux系列(42) - 防火墙相关命令

    # 开启 service firewalld start # 重启 service firewalld restart # 关闭 service firewalld stop # 查看防火墙规则 fi ...

  10. 发送curl请求的函数

    //发送curl请求的函数function curl_request($url, $post = false, $data=array(), $https = false){ //使用curl_ini ...