NLog配置文件变量

${basedir}:应用程序当前目录
${shortdate}:短日期【2022-01-06】
${longdate}:长日期【2022-01-06 14:05:20.4023】
${logger}: 记录器的名字
${event-properties:FileName}:自定义属性
${level}:记录等级【Trace,Debug,Info,Warn,Error,Fatal】
${uppercase}:转大写
${message}:调用Nlog时输入的内容
${callsite}:类名称
${callsite-linenumber}:文件行号
${hostname}:主机名
${environment} 环境变量
${exception} exception信息
${machinename} 名称
${mdc} 映射诊断
${mdlc} 异步映射诊断上下文
${ndc} 线程结构
${ndlc} 异步线程
${newline} 文字换行
${nlogdir} nlog.dll目录。
${performancecounter} 述性能计数器。
${processid} 当前进程标识符
${processinfo} 运行信息
${processname} 当前进程的名称。
${processtime} 该时间过程中格式HH:MM:ss.mmm。
${qpc} 高精度定时器,基于返回的值从queryperformancecounter(任选地)转换为秒。
${registry} 从注册表中的值。
${sequenceid} ID
${shortdate} 短时间 格式YYYY-MM-DD。
${sl-appinfo} Silverlight应用。
${specialfolder} 文件夹路径
${stacktrace} - 堆栈跟踪渲染器。
${tempdir} 临时目录中。
${threadid} 当前线程的标识符。
${threadname} 当前线程。
${ticks} 当前日期和时间。
${time} 24小时格式HH:MM:ss.mmm。
KaTeX parse error: Expected '}', got 'EOF' at end of input: {var} {var}-提供新的变量(4.1)
${windows-identity} indows线程标识信息(用户名)
Windows Form
<PackageReference Include="NLog" Version="5.2.2" />
<PackageReference Include="NLog.Windows.Forms" Version="5.2.0" />

Web Api
<PackageReference Include="NLog.Web.AspNetCore" Version="5.2.1" />

using Microsoft.Extensions.Logging;

 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" autoReload="true" internalLogLevel="Info">
  <!-- 启用.net core的核心布局渲染器 -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore" />
  </extensions>
  <!-- 写入日志的目标配置 -->
  <targets>
    <!-- 调试  -->
    <target xsi:type="File" name="debug" fileName="logs/debug-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
    <!-- 警告  -->
    <target xsi:type="File" name="warn" fileName="logs/warn-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
    <!-- 错误  -->
    <target xsi:type="File" name="error" fileName="logs/error-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
  </targets>
  <!-- 映射规则 -->
  <rules>
    <!-- 调试  -->
    <logger name="*" minlevel="Trace" maxlevel="Debug" writeTo="debug" />
    <!--跳过不重要的微软日志-->
    <logger name="Microsoft.*" maxlevel="Info" final="true" />
    <!-- 警告  -->
    <logger name="*" minlevel="Info" maxlevel="Warn" writeTo="warn" />
    <!-- 错误  -->
    <logger name="*" minlevel="Error" maxlevel="Fatal" writeTo="error" />
  </rules>
</nlog>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NLog 是一个常用的 .NET 日志记录库,它支持多种日志记录方式,包括文件、数据库、网络等。下面是一个简单的 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"> <targets> <target name="logfile" xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" /> </targets> <rules> <logger name="*" minlevel="Info" writeTo="logfile" /> </rules> </nlog> ``` 在上面的配置文件中,我们定义了一个名为 "logfile" 的目标,类型为文件(xsi:type="File"),文件名为 ${basedir}/logs/${shortdate}.log。这里的 ${basedir} 表示程序运行的根目录,${shortdate} 表示日期,例如 2022-05-09。 然后,我们又定义了一个规则,匹配所有的日志记录器(name="*"),最低日志级别为 Info(minlevel="Info"),并将日志记录到 "logfile" 目标中(writeTo="logfile")。 最后,我们需要在代码中加载这个配置文件,以便日志记录器使用它: ```csharp var logger = LogManager.GetCurrentClassLogger(); ``` 这里的 LogManager 是 NLog 库中的一个静态类,它用于管理日志记录器。GetCurrentClassLogger() 方法用于获取当前类的日志记录器。如果没有指定日志记录器的名称,则默认使用当前类的名称作为日志记录器的名称。 在实际应用中,我们可以根据需要修改配置文件,以实现不同的日志记录方式,例如数据库、网络等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值