• 日志框架 NLog


    这里按老规矩先进行和其它产品进行比较:

    目前在.net平台存在两个比较老牌的日志框架分别为Log4net和NLog。

    我们进行对这两种框架进行比较下

    Log4net

    Log4net是一个老牌的日志框架,它可以实现日志多目标输出,输出到控制台、文件、数据库、系统事件、Email等,几乎无所不能。它可以通过配置让日志系统实时生效,比如说在服务运行的过程中修改配置改变输出目标,改变日志等级等,均不用重启程序。存在的缺点就是配置繁琐,基本上不查资料我是配置不出来的。

    NLog

    NLog是和Log4net差不多的日志框架,属于新兴的日志框架。我们从它们的支持和性能方面来比较下。

    借鉴博客园大佬们测试的结果

    我原先差点就信了,后来作者又在2017年9月1日发出以下内容。

    以上性能测试不再有效,根据网友反馈,由于输出内容的略有差异导致上面性能测试出现不公平的情况,log4Net在输出时可能会有更多的计算量。在优化测试代码情况下,仅让日志框架打印日志内容,其余的包括时间、日志等级、日志类名一律不打印,使用最新版Dll,两个框架性能相差无几。

    毕竟是老牌子的日志框架,居然还输给新人啦。

    但从更新速度来说,Nlog一直处于持续更新中,支持的也比较多。所以我们还是在这里讲下。

    先用nuget 加载下NLog

    加载后我们来看下项目里面多了些神马?

    日志的配置(摘抄)

    通过在启动的时候对一些常用目录的扫描,NLog会尝试使用找到的配置信息进行自动的自我配置。当你运行一个独立的*.exe客户端可执行程序时,NLog将在以下目录搜索配置信息:

    标准的程序配置文件(通常为 程序名.exe.config

    程序目录下的程序名.exe.nlog文件

    程序目录下的NLog.config文件

    NLog.dll所在目录下的NLog.dll.nlog文件

    如果定义了NLOG_GLOBAL_CONFIG_FILE环境变量,则该变量所指向的文件

    如果是一个ASP.NET程序,被搜索的目录包括:

    标准的web程序配置文件web.config

    web.config在同一目录下的web.nlog文件

    程序目录下的NLog.config文件

    NLog.dll所在目录下的NLog.dll.nlog文件

    如果定义了NLOG_GLOBAL_CONFIG_FILE环境变量,则该变量所指向的文件

    由于.NET Compact Framework不支持程序配置文件(*.exe.config)和环境变量,因此NLog将只会扫描这些地方:

    程序目录下的NLog.config文件

    NLog.dll所在目录下的NLog.dll.nlog文件

    如果定义了NLOG_GLOBAL_CONFIG_FILE环境变量,则该变量所指向的文件

     

    NLog配置文件NLog.config,通过上述内容我们可以知道NLog可以配置在web.config里面或者独立的NLog.config文件都是OK的。

    Web.config

    <configuration>

    <configSections>

    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>

    </configSections>

    <nlog>

    这部分就是NLog.config里面内容

    </nlog>

    </configuration>

     

    Nlog.config

    <?xml version="1.0" encoding="utf-8" ?>

    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"

    autoReload="true"

    throwExceptions="false"

    internalLogLevel="Off" internalLogFile="c: emp log-internal.log">

    <!-- optional, add some variables

    https://github.com/nlog/NLog/wiki/Configuration-file#variables

    -->

    <variable name="myvar" value="myvalue"/>

    <!--

    See https://github.com/nlog/nlog/wiki/Configuration-file

    for information on customizing logging rules and outputs.

    -->

    <targets>

    <!--

    add your targets here

    See https://github.com/nlog/NLog/wiki/Targets for possible targets.

    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.

    -->

    <!--

    Write events to a file with the date in the filename.

    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"

    layout="${longdate} ${uppercase:${level}} ${message}" />

    -->

    </targets>

    <rules>

    <!-- add your logging rules here -->

    <!--

    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"

    <logger name="*" minlevel="Debug" writeTo="f" />

    -->

    </rules>

    </nlog>

    我们对上述节点名称进行解释下:

    1. <targets>我们看下config里面的描述可以知道是用来配置日志目标/输出

      每个< targets >代表一个目标,且< targets >带有两个属性分别为name和type.

    • name - 目标的名称
    • type - 目标的类型 - 比如"File","Database","Mail"。如果你使用了名字空间,这个属性会被命名为 xsi:type.

     

    1. <rules>看下描述,添加你的日志路由规则,Debug,Info, Warn, Error and Fatal
    2. <variable> 配置变量赋值
    3. <include /> - 导入外部配置文件

      这里我就不详细描述内容,因为官网已经说的很详细了,并且有例子。

      https://github.com/NLog/NLog/wiki

  • 相关阅读:
    twemproxy配置
    tomcat远程调试
    hadoop配置
    kafka原理分析
    hive-sql
    P1983 车站分级
    拓扑排序
    洛谷P1982 小朋友的数字
    字典树Trie
    城市交通费
  • 原文地址:https://www.cnblogs.com/Wtomato/p/7525985.html
Copyright © 2020-2023  润新知