2017-05-03 71 views
0

我使用的是使用NLog.Web.AspNetCore(4.3.1)nuget软件包的完整.net框架4.6.2的asp.net核心项目。以下是我的配置。NLog不记录(使用.net 4.6.2的Asp.net核心)

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="Warn" 
    internalLogFile="c:\Logs\internal-nlog.txt"> 
    <!-- define various log targets --> 
<targets> 
    <!-- write logs to file --> 
    <target xsi:type="File" name="allfile" fileName="c:\Logs\nlog-all-${shortdate}.log" 
     layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception:format=stacktrace}" 
     maxArchiveFiles="100" 
     archiveFileName="c:\LogsBackup\nlog-all-${shortdate}.{##}.log" 
     archiveNumbering="DateAndSequence" 
     archiveAboveSize="10000000" 
     archiveDateFormat="yyyyMMdd" /> 
    <target xsi:type="File" name="ownFile-web" fileName="c:\Logs\nlog-own-${shortdate}.log" 
     layout="${longdate}|${logger}|${uppercase:${level}}| ${message} ${exception:format=stacktrace}" /> 
    <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-web" />--> 
</rules> 
</nlog> 

Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
     loggerFactory.AddDebug(); 

     //add nlog 
     //env.ConfigureNLog("nlog.config"); 
     //loggerFactory.ConfigureNLog("nlog.config"); 
     //loggerFactory.AddNLog(); 
     app.AddNLogWeb(); 

     //add identity server authentication 
     //app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions 
     //{ 
     // Authority = "http://localhost:5000", 
     // RequireHttpsMetadata = false, 
     // ApiName = "prismapi" 
     //}); 

     app.UseMvcWithDefaultRoute(); 

     app.UseSwagger(); 
     app.UseSwaggerUI(options => 
     { 
      options.SwaggerEndpoint("/swagger/v1.0/swagger.json", "Prism api version 1"); 
     }); 
    } 

出于某种原因,其不发射日志。 Nlog甚至没有创建它的内部日志文件。有趣的是,如果我取消注释env.ConfigureNLog("nlog.config");它开始创建至少它的内部日志文件,但仍然没有成功与实际的日志文件。

回答

1

可能您需要在您的Configure方法中取消loggerFactory.AddNLog();之前app.AddNLogWeb();的注释。

env.ConfigureNLog("nlog.config");应根据文档中的示例在构造函数中。

也请看看How to usehttps://github.com/NLog/NLog.Extensions.Logging

我也想建议,开始与相同的代码行作为例子。如果这是行之有效的,请根据您的需求改变它

希望这会有所帮助。

+0

它确实有帮助,非常感谢:) –

相关问题