NLog不会创建日志文件

时间:2016-05-24 08:07:02

标签: asp.net nlog

我刚刚创建了一个空的ASP.NET 5 Web应用程序,我想将NLog用于我的日志。到目前为止,我已成功安装NLog并在Startup类中创建了一个公共静态Logger。在我的应用程序文件夹中,我创建了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">
    <targets>
        <target name="logfile" xsi:type="File" fileName="file.txt" />
    </targets>
    <rules>
        <logger name="*" minlevel="Info" writeTo="logfile" />
    </rules>
</nlog>

在Startup类的Configure方法中,我尝试使用以下代码行记录一些数据:

logger.Warn("foo1");
logger.Debug("foo2");

项目构建成功但运行后我没有看到任何file.txt。

3 个答案:

答案 0 :(得分:1)

我找到了一个可能的解决方案。创建ASP.NET 5空项目后,您可以执行以下操作:

1)打开Startup.cs文件并添加以下用法:

using Microsoft.Extensions.Logging;
using NLog.Framework.Logging;

请注意,为了添加第二个,您必须通过添加以下依赖项来修改package.json文件:

"NLog.Framework.logging": "1.0.0-rc1-final"

2)在startup.cs文件中,您必须修改&#34;配置&#34;的签名。通过添加参数ihe和ilf的方法。所以它最终会是这样的。

Configure(IApplicationBuilder app, IHostingEnvironment ihe, ILoggerFactory ilf)

以这种方式使用ihe和ifg的参数:

ilf.AddNLog();
ihe.ConfigureNLog("nlog.config");
ILogger logger = ilf.CreateLogger(GetType().Namespace);
logger.LogInformation("i am nlog bye bye");

3)在项目文件夹中,这不是wwwroot而是其父文件夹,添加一个文件并调用它&#34; nlog.config&#34;。它可以填充以下内容:

<?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="Warn"
      internalLogFile="c:\temp\internal.txt">


  <!-- define various log targets -->
  <targets>
    <!-- write logs to file -->
    <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
                 layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />

    <target xsi:type="File" name="ownFile" fileName="c:\temp\nlog-own-${shortdate}.log"
              layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />

    <target xsi:type="Null" name="blackhole" />
  </targets>

  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Trace" writeTo="ownFile" />
  </rules>
</nlog>

现在,如果您运行项目并转到C:\ temp,您将看到两个文件:&#34; nlog-all- .log&#34;和&#34; nlog-own - .log&#34;。在第二个文件中,您将看到消息&#34;我是再见&#34;

答案 1 :(得分:0)

提供日志文件的完整路径

<?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">

  <!-- 
  See http://nlog-project.org/wiki/Configuration_file 
  for information on customizing logging rules and outputs.
   -->
  <targets>
    <!-- add your targets here -->
    <target name="asyncFile" xsi:type="AsyncWrapper">
      <target name="logfile" xsi:type="File" fileName="C:\Log.txt" layout="${longdate} ${callsite} ${level} ${message} ${newline}" />
    </target>
    <!--
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->
  </targets>

  <rules>
    <!-- add your logging rules here -->
    <logger name="*" minlevel="Info" writeTo="asyncFile" />
    <!--
    <logger name="*" minlevel="Trace" writeTo="f" />
    -->
  </rules>
</nlog>

答案 2 :(得分:0)

这是我使用过的NLogConfiguration,它的工作很精细

 <?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">
        <targets >
    <target xsi:type="File" name="logfile"
            fileName="${basedir}/logs/YourFileName.log"
            layout="${longdate} ${level:uppercase=true:padding=5} ${gdc:item=hostname} ${gdc:item=useremail} (${logger} - ${mdc:item=actionname})  ${message} ${exception:format=tostring}"
            archiveEvery="Day"
            archiveFileName ="${basedir}/logs/YourFileName.${date:format=yyyy-MM-dd HH.mm}.{#}.log"
            archiveNumbering ="Sequence"
            maxArchiveFiles="30"
            fileAttributes="Compressed">
    </target>
       </targets>
       <rules>
       <logger name="*" minlevel="Debug" writeTo="logfile">
      </logger>
      </rules>
    </nlog>

并在服务器端

public class YourClassName
{
   private static Logger log = LogManager.GetCurrentClassLogger();

  public void YourMethodName()
    {
      try
        {
          log.Info("Success");
        }
        catch(Exception Ex)
        {
         log.Info("Exception"+ex);
        }
       } 
   }