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");
它開始創建至少它的內部日誌文件,但仍然沒有成功與實際的日誌文件。
它確實有幫助,非常感謝:) –