Reputation: 83
Type: Bug
NLog version: 4.7.2
NLog.Web/NLog.Web.AspNetCore version: 4.9.2
NLog.Extensions.Logging version: N/A
Platform: .NET Core 3.1
Current NLog config (xml or C#, if relevant)
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="internal.txt">
<extensions>
<!--enable NLog.Web for ASP.NET Core-->
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- define various log targets -->
<targets>
<target xsi:type="File"
name="allOutput"
fileName="${startupdir}\logs\SkyJack-Agent-All-${shortdate}.log"
layout="${longdate} ${logger} ${uppercase:${level}} ${message} ${exception}" />
<target xsi:type="File"
name="ourOutput"
fileName="${startupdir}\logs\SkyJack-Agent-OurOutput-${shortdate}.log"
layout="${longdate} ${logger} ${uppercase:${level}} ${message} ${exception}" />
<target xsi:type="File"
name="errorsOnly"
fileName="${startupdir}\logs\SkyJack-Agent-ErrorsOnly-${shortdate}.log"
layout="${longdate} ${logger} ${uppercase:${level}} ${message} ${exception}" />
<target name="console" xsi:type="ColoredConsole" layout="${longdate} ${pad:padding=5:inner=-${level}-} ${message}"
useDefaultRowHighlightingRules="false" >
<highlight-row condition="level==LogLevel.Trace" foregroundColor="Gray" />
<highlight-row condition="level==LogLevel.Debug" foregroundColor="DarkGray" />
<highlight-word text="-Info-" foregroundColor="DarkGreen" />
<highlight-word text="-Warn-" foregroundColor="DarkYellow" />
<highlight-word text="-Error-" foregroundColor="DarkMagenta" />
<highlight-word text="-Fatal-" foregroundColor="DarkRed" backgroundColor="White"/>
</target>
</targets>
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Debug" writeTo="console" />
<logger name="*" minlevel="Trace" writeTo="allOutput" />
<logger name="*" minlevel="Error" writeTo="errorsOnly" />
<!--Skip Microsoft logs - no writeTo attribute-->
<logger name="Microsoft.*" minlevel="Trace" final="true" />
<logger name="*" minlevel="Trace" writeTo="ourOutput" />
</rules>
</nlog>
I am using NLog in a .NET Core web service published as single file and attempting to install it as a service. NLog works fine when I run it normally, however when running as a service it needs to be told where to find the config file, which I am accomplishing with:
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(Path.Combine(pathToContentRoot, "nlog.config"));
var logger = NLog.Web.NLogBuilder.ConfigureNLog(NLog.LogManager.Configuration).GetCurrentClassLogger();
And the content root with:
builder.UseWindowsService().UseContentRoot(pathToContentRoot);
This appears to work, at least at first, and generates some log output, enough to see that I am setting the content root successfully:
2020-05-28 15:42:20.5932 SkyJack.Agent.Program INFO ... Initializing SkyJack Agent with content root G:\MSGPS2\SkyJack\SkyJack\src\SkyJack.Agent\bin\Release\netcoreapp3.1\publish...
2020-05-28 15:42:20.6373 SkyJack.Agent.Program TRACE Entering CreateWebHostBuilder()
2020-05-28 15:42:20.8023 SkyJack.Agent.Program TRACE Returned from CreateWebHostBuilder()
2020-05-28 15:42:20.8023 SkyJack.Agent.Program TRACE Entering builder.Build()
2020-05-28 15:42:20.9446 SkyJack.Agent.Program TRACE Returned from builder.Build()
Eventually, however, it throws an exception:
Log Name: Application
Source: ServerManagerAgent
Date: 5/28/2020 3:54:04 PM
Event ID: 6
Task Category: None
Level: Error
Keywords: Classic
User: N/A
Computer: HPZEE.redmond.corp.microsoft.com
Description:
Category: Microsoft.AspNetCore.Hosting.Diagnostics
EventId: 6
Application startup exception
Exception:
System.IO.FileNotFoundException: Failed to load NLog LoggingConfiguration. Searched the following locations:
- C:\WINDOWS\TEMP\.net\ServerManagerAgent\31dyvwga.rkl\nlog.config
File name: 'nlog.config'
at NLog.LogFactory.LoadConfiguration(String configFile, Boolean optional)
at NLog.LogFactory.LoadConfiguration(String configFile)
at NLog.LogManager.LoadConfiguration(String configFile)
at NLog.Web.NLogBuilder.ConfigureNLog(String configFileName)
at WebAppService.Startup.Configure(IApplicationBuilder app)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app)
at Microsoft.Extensions.DependencyInjection.AutoRegisterMiddleware.<>c__DisplayClass4_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="ServerManagerAgent" />
<EventID Qualifiers="0">6</EventID>
<Version>0</Version>
<Level>2</Level>
<Task>0</Task>
<Opcode>0</Opcode>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2020-05-28T22:54:04.0041305Z" />
<EventRecordID>61154</EventRecordID>
<Correlation />
<Execution ProcessID="0" ThreadID="0" />
<Channel>Application</Channel>
<Computer>HPZEE.redmond.corp.microsoft.com</Computer>
<Security />
</System>
<EventData>
<Data>Category: Microsoft.AspNetCore.Hosting.Diagnostics
EventId: 6
Application startup exception
Exception:
System.IO.FileNotFoundException: Failed to load NLog LoggingConfiguration. Searched the following locations:
- C:\WINDOWS\TEMP\.net\ServerManagerAgent\31dyvwga.rkl\nlog.config
File name: 'nlog.config'
at NLog.LogFactory.LoadConfiguration(String configFile, Boolean optional)
at NLog.LogFactory.LoadConfiguration(String configFile)
at NLog.LogManager.LoadConfiguration(String configFile)
at NLog.Web.NLogBuilder.ConfigureNLog(String configFileName)
at WebAppService.Startup.Configure(IApplicationBuilder app)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app)
at Microsoft.Extensions.DependencyInjection.AutoRegisterMiddleware.<>c__DisplayClass4_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
</Data>
</EventData>
</Event>
WebAppService.Startup.Configure() is calling NLog.Web.NLogBuilder.ConfigureNLog() again, presumably with just "nlog.config" instead of the location or LoggerConfiguration object that I passed in to begin with.
Is there any way to get it to use the location or configuration that I've already specified and loaded?
Upvotes: 1
Views: 5098
Reputation: 83
I got it working with:
NLog.LogManager.LogFactory.SetCandidateConfigFilePaths(new List<string> { $"{Path.Combine(pathToContentRoot, "nlog.config")}" });
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
Upvotes: 4