NLog小记

NLog安装:

Install-Package NLog

NLog配置:

复制代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

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

  <!--autoReload="true"表示在不重新启动应用程序的情况下,修改配置文件,NLog会自动加载应-->
  <!--NLog内部的日志消息写到应用程序目录下的logs文件夹里的internalLog.txt文件中;(-->
  <nlog autoReload="true" internalLogLevel="Trace" internalLogFile="logs/internalLog.txt">
    <targets>
      <!--文件输出(type="File")-->
      <target name="FileOutput" type="File" fileName="${basedir}/logs/${shortdate}.log"
              layout="${longdate} ${callsite} ${level}:
              ${message} ${event-context:item=exception} ${stacktrace} ${event-context:item=stacktrace}" />
      <!--控制台输出(type="File")-->
      <target name="ConsoleOutput" type="Console" layout="${date:format=yyyy-MM-dd HHmmss} ${callsite} ${level} ${message}"></target>
    </targets>
    <rules>
      <!--<logger name="Test" minlevel="Debug" maxlevel="Error" writeTo="FileOutput,ConsoleOutput" />-->
      <logger name="*" minlevel="Debug" maxlevel="Error" writeTo="FileOutput,ConsoleOutput" />
    </rules>
  </nlog>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>
View Code
复制代码

NLog使用:

复制代码
 private static readonly Logger Logger = LogManager.GetCurrentClassLogger();//LogManager.GetLogger("Test");
        static void Main(string[] args)
        {
            Logger.Trace("This is Trace");
            Logger.Debug("This is Debug");
            Logger.Info("This is Info");
            Logger.Error("This is Error");
            Logger.Fatal("This is Fatal");

            Console.ReadLine();
        }
复制代码

 

posted @   狂想NICE  阅读(175)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示