• 日志服务:NLog


    ylbtech-日志服务:NLog
    NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码。
    NLog是一个简单灵活的.NET日志记录类库。通过使用NLog,我们可以在任何一种.NET语言中输出带有上下文的(contextual information)调试诊断信息,根据喜好配置其表现样式之后发送到一个或多个输出目标(target)中。
    NLog的API非常类似于log4net,且配置方式非常简单。NLog使用路由表(routing table)进行配置,但log4net却使用层次性的appender配置,这样就让NLog的配置文件非常容易阅读,并便于今后维护。
    NLog遵从BSD license,即允许商业应用且完全开放源代码。任何人都可以免费使用并对其进行测试,然后通过邮件列表反馈问题以及建议。
    NLog支持.NET、C/C++以及COM interop API,因此我们的程序、组件、包括用C++/COM 编写的遗留模块都可以通过同一个路由引擎将信息发送至NLog中。
     
    1.返回顶部
    1、
    NLog允许我们自定义从跟踪消息的来源(source)到记录跟踪信息的目标(target)的规则(rules)。记录跟踪信息的目标(target)可以为如下几种形式:
    1. 文件
    2. 文本控制台
    3. Email
    4. 数据库
    5. 网络中的其它计算机(通过TCP或UDP)
    6. 基于MSMQ的消息队列
    7. Windows系统日志
    除此之外,每一条跟踪消息都可以自动带有上下文信息(contextual information),并将其发送给记录跟踪信息的目标。这些上下文信息可以包含如下内容:
    1. 当前的日期和时间(多种格式)
    2. 记录等级
    3. 来源名称
    4. 输出跟踪消息的方法的堆栈信息
    5. 环境变量的值
    6. 异常的详细信息
    7. 计算机、进程和线程名称
    每条跟踪信息都包含一个记录等级(log level)信息,用来描述该条信息的重要性。NLog支持如下几种记录等级:
    1. Trace- 最常见的记录信息,一般用于普通输出
    2. Debug- 同样是记录信息,不过出现的频率要比Trace少一些,一般用来调试程序
    3. Info- 信息类型的消息
    4. Warn- 警告信息,一般用于比较重要的场合
    5. Error- 错误信息
    6. Fatal- 致命异常信息。一般来讲,发生致命异常之后程序将无法继续执行。
    NLog的.NET API的过滤信息功能执行效率很高,这样我们就可以一直保留程序中的日志写入代码,然后由NLog在运行时将其根据需要过滤掉。在一个1.6G单CPU笔记本电脑上,NLog每秒钟可以过滤掉1.5亿条日志写入语句!加上异步处理(asynchronous processing)以及其他包装程序(wrappers)的支持,NLog将成为一个极为强大的、且极具伸缩性的日志记录工具,
    2、
    2.返回顶部
    1、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">
    
      <!-- 
      See https://github.com/nlog/nlog/wiki/Configuration-file 
      for information on customizing logging rules and outputs.
       -->
      <targets>
        <default-wrapper xsi:type="AsyncWrapper">
          <wrapper-target xsi:type="RetryingWrapper"/>
        </default-wrapper>
        <!-- add your targets here -->
        <target xsi:type="File" name="AllLog" fileName="${basedir}/App_Data/logs/${shortdate}/all.log" layout="${longdate} ${uppercase:${level}} [${threadid}] ${logger} - ${message} ${exception:format=tostring}" />
        <target xsi:type="File" name="DebugLog" fileName="${basedir}/App_Data/logs/${shortdate}/debug.log" layout="${longdate} [${threadid}] ${logger} - ${message} ${exception:format=tostring}" />
        <target xsi:type="File" name="DelayDebug" fileName="${basedir}/App_Data/logs/${shortdate}/debug.log" layout="${longdate}-${message} ${exception:format=tostring}" />
        <target xsi:type="File" name="ErrorLog" fileName="${basedir}/App_Data/logs/${shortdate}/error.log" layout="${longdate} [${threadid}] ${logger} - ${message} ${exception:format=tostring}" />
        <target xsi:type="File" name="FatalLog" fileName="${basedir}/App_Data/logs/${shortdate}/fatal.log" layout="${longdate} [${threadid}] ${logger} - ${message} ${exception:format=tostring}" />
        <target xsi:type="File" name="InfoLog" fileName="${basedir}/App_Data/logs/${shortdate}/info.log" layout="${longdate} [${threadid}] ${logger} - ${message} ${exception:format=tostring}" />
        <target xsi:type="File" name="TraceLog" fileName="${basedir}/App_Data/logs/${shortdate}/trace.log" layout="${longdate} [${threadid}] ${logger} - ${message} ${exception:format=tostring}" />
        <target xsi:type="File" name="WarnLog" fileName="${basedir}/App_Data/logs/${shortdate}/warn.log" layout="${longdate} [${threadid}] ${logger} - ${message} ${exception:format=tostring}" />
    
        <target xsi:type="NLogViewer"
                name="viewer"
                address="udp://127.0.0.1:9999"/>
      </targets>
    
      <rules>
        <!-- add your logging rules here -->
        <logger name="*" minlevel="Trace" writeTo="AllLog" />
        <logger name="*" level="Debug" writeTo="DelayDebug" />
        <logger name="*" level="Error" writeTo="ErrorLog" />
        <logger name="*" level="Fatal" writeTo="FatalLog" />
        <logger name="*" level="Info" writeTo="InfoLog" />
        <logger name="*" level="Trace" writeTo="TraceLog" />
        <logger name="*" level="Warn" writeTo="WarnLog" />
        <logger name="*"
                    minlevel="Trace"
                    writeTo="viewer" />
      </rules>
    </nlog>
    2、
    3.返回顶部
     
    4.返回顶部
     
    5.返回顶部
    0、
    1、
    2、
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    pandas 查看行列数
    git提交代码至GitHub
    编码错误
    Pandas中根据列的值选取多行数据
    pandas过滤缺失数据之dropna()
    kail linux安装
    mybatis_plus插件使用
    Vue 插槽和自定义事件
    Vue计算属性
    使用axios 异步显示数据到页面
  • 原文地址:https://www.cnblogs.com/storebook/p/9138617.html
Copyright © 2020-2023  润新知