簡體   English   中英

如何在我的班級C#中保存NLog.config

[英]How to save NLog.config in my class c#

我想將Nlog錯誤保存在文件中。如何在我的方法中使用NLog.config? 將日志錯誤保存在存檔文件夾下為

2010-06-05.log
2010-06-06.log
2010-06-07.log
2010-06-08.log
...

NLOG.config中

<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <targets>
    <target name="file" xsi:type="File"
        layout="${longdate} ${logger} ${message}" 
        fileName="${basedir}/${shortdate}.log" />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="file" />
  </rules>

</nlog>

在我班上

namespace MyNamespace
{
    public class MyClass
    {
       public void Method()
       {
           /* How do I save errors to an NLog file in here ?? */
       }
     }
}

您應該實例化Logger instance (通常每個類一個)。 然后,您可以調用各種可用的日志記錄方法,例如Error()Warning()Info()Debug() ,以適當的級別記錄所需的任何內容。

namespace MyNamespace
{
    using NLog;

    public class MyClass
    {
       private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

       public void Method()
       {
           Logger.Error("My error message");
       }
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM