NLog简单使用教程

NLog简单使用教程

NLog介绍

NLog是一个记录日志的模块,简单配置就能使用。

如何获得

在visual studio里面的NuGet程序包管理器里面直接搜NLog 和 NLog.config添加上就可以了。这里稍微说下,NLog是模块主体,NLog.config是一个配置文件,如果你对NLog足够了解可以自己配置文件。

配置文件

这是一个配置文件例子

<?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:\temp\nlog-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="File" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />

    <target xsi:type="ColoredConsole"
          name="ColoredConsole"
          layout="${longdate}|${level:uppercase=true}|${logger}|${message}">
    </target>
    
  </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="File,ColoredConsole" />
   
  </rules>
</nlog>

配置文件分为 名空间 变量 日志输出目标 和 日志规则四个部分,我们慢点说。

名空间

<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:\temp\nlog-internal.log">

这部分代码定义的是这个xml文件的内容。大意是这是一个符合xml规则的一个xml文件。望文知意。

变量

<!-- 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="File" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />

    <target xsi:type="ColoredConsole"
          name="ColoredConsole"
          layout="${longdate}|${level:uppercase=true}|${logger}|${message}">
    </target>
    
  </targets>

日志输出目标就是说日志要输出到什么地方,这里可以指定很多地方,控制台,文件,数据库,通过邮件发送等,也可以自定义输出目标,不过需要用到NLog的扩展去自己实现,我这里的代码是添加了文件和一个带有颜色的控制台输出目标。
这里有个布局Layout可以说下,就是日志的输出格式,有参考链接,可以自己定制需要的输出格式。

日志规则

<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="File,ColoredConsole" />
<logger name="MMM" minlevel="Debug" writeTo="File,ColoredConsole" />
   
  </rules>

程序中会定义一些日志实例-logger,日志规则定义日志实例输出到那些日志目标,这段代码的意思是所有的日志实例输出都会输出到颜色控制台和文件,这样在程序运行时和运行后都可以看到。

代码

LogManager.GetCurrentClassLogger().Log(LogLevel.Error, "XXXX");
LogManager.GetLogger("MMM ").Log(LogLevel.Error, "xxxxxxxxxxxxxxxx");

每一个调用类可以自动生成一个日志实例,也可以获取设置的日志实例来记录日志

总结

写这个的目的是为将来如果用NLOG直接拷贝配置代码就可以直接使用了。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在 .NET Core 控制台应用中使用 NLog,需要进行以下步骤: 1. 安装 NLog 包。您可以在 NuGet 包管理器中搜索 NLog 并安装它,或使用命令行: ``` dotnet add package NLog ``` 2. 在项目根目录下创建一个名为 `nlog.config` 的文件,并将以下配置复制到文件中,以启用NLog: ```xml <?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" autoReload="true" internalLogLevel="Trace" internalLogFile="c:\temp\nlog-internal.log"> <targets> <target name="file" xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> </targets> <rules> <logger name="*" minlevel="Trace" writeTo="file" /> </rules> </nlog> ``` 这样,NLog 就会将日志写入到当前应用程序目录下的 `logs` 文件夹中,并将日志文件名设置为当前日期。 3. 在应用程序入口点中,添加 NLog 配置和日志记录: ```csharp using NLog; using NLog.Config; using NLog.Targets; class Program { static void Main(string[] args) { // 加载NLog配置 LogManager.LoadConfiguration("nlog.config"); // 创建logger var logger = LogManager.GetCurrentClassLogger(); // 记录日志 logger.Info("Hello, NLog!"); // 等待用户按下任意键退出 Console.ReadKey(); } } ``` 这样就可以在控制台应用程序中使用 NLog 记录日志了。如果您需要更多的日志记录选项,可以参考 NLog 的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当当小螳螂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值