代码之家  ›  专栏  ›  技术社区  ›  David

NLog在c中获取日志消息#

  •  0
  • David  · 技术社区  · 6 年前

    我怎样才能得到以下信息?

    string message = logger.getLog() <---- This is what I need
    
    4 回复  |  直到 6 年前
        1
  •  2
  •   Julian    6 年前

    在这种情况下,最好(IMO)使用内存目标。 (docs memory target)

    配置:

    <targets>  
        <target name="target1" xsi:type="Memory" layout="${message}"/>  
        <target name="target2" xsi:type="File" fileName="C:\log\NLog.log" layout="${longdate}|${message}"/>  
    </targets>  
    
    <rules>  
      <logger name="*" minlevel="Error" writeTo="target1,target2" />  
    </rules> 
    

    日志消息:

     LogManager.GetLogger("logger1").Info("my log message");
    

    检索:(另见: MemoryTarget class - API docs

    var target = LogManager.Configuration.FindTargetByName<MemoryTarget>("target1");
    IList<string> logs = target.Logs; 
    // show logs etc.
    // delete if not needed any more: target.Logs.Clear()
    
        3
  •  0
  •   Robyn    6 年前

    你可以为NLog记录器写一个包装。所以你打电话来大卫斯洛格日志(error)并且该方法将日志消息存储在您可以访问的内存中,然后调用NLog上的常用方法将消息永久存储在日志中。

        4
  •  0
  •   Gauravsa    6 年前

    targets rules 在Nlog.Config文件如下所示:

    <targets>  
        <target name="console" xsitype="Console" layout="${longdate}|${message}"/>  
         <target name="file" xsitype="File" fileName="C:\log\NLog.log" layout="${longdate}|${message}"/>  
    </targets>  
    
    <rules>  
      <logger name="*" minlevel="Error" writeTo="console,file" />  
    </rules>  
    

    这将把日志写入控制台和文件。您也可以将日志发送到电子邮件。这个链接可以让你对NLog有一个基本的了解:

    https://www.c-sharpcorner.com/article/basic-understanding-of-nlog/

    在这里: https://github.com/nlog/NLog/wiki/Configuration-file

    以下是级别的“优先级”(从上面的链接复制):

    • 致命的最高等级:重要的东西下来
    • 错误,例如应用程序崩溃/异常。
    • 信息正常行为,如邮件发送,用户更新配置文件等。
    • 调试已执行的查询,用户已验证,会话已过期
        5
  •  0
  •   Dominic Jonas    5 年前

    你可以用我的 NLogViewer https://github.com/dojo90/NLogViewer ). 它是一个 wpf 控制。我知道你写的 win forms 但也许你将来会需要它。

    还提供nuget软件包-> https://www.nuget.org/packages/Sentinel.NLogViewer

    enter image description here