I am publishing a .NET Core 2.2 app to an Azure App Service. I've configured the app service to save logs to an Azure Storage blob. I'm trying to swap over to using NLog since I wasn't able to see scopes in those logs.
I've followed the guide on NLog's documentation; added the PackageReferences in the .csproj, altered Program.cs as instructed and removed the call to AddLogging in Startup.cs. When I debug my application locally, I can see NLog outputting what I expect in my debug log. When I publish, the log stream still displays the same way it used to. The log files in my blob storage also do not appear to have been formatted by NLog.
The internal NLog file is not reporting any issues with my targets. I can use remote debugging and I can see NLog is writing to debug.
Excerpt of my csproj
<ItemGroup>
<None Include="NLog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
</ItemGroup>
My 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="Info"
internalLogFile="./internal-nlog.txt"
internalLogToConsole="true"
internalLogToConsoleError="true"
internalLogToTrace="true"
internalLogIncludeTimestamp="true"
throwExceptions="true"
throwConfigExceptions="true">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore" />
</extensions>
<time type="FastLocal" />
<!-- the targets to write to -->
<targets>
<!-- 2020-04-02 20:00:12.062 +00:00 [Trace] Hangfire.Server.ServerJobCancellationWatcher: No newly aborted jobs found. -->
<target xsi:type="Console"
name="ConsoleTarget"
layout="> ${longdate:universalTime=true} [${level}] ${logger}.${callsite:className=false}:${when:when=length('${ndlc}')>0:inner= [${ndlc}]} ${message} ${exception:format=tostring}"
encoding="utf-8"
error="false"
detectConsoleAvailable="true" />
<target xsi:type="File"
name="allfile"
fileName=".\nlog-all-${shortdate}.log"
layout="${longdate:universalTime=true} [${level}] ${logger}.${callsite:className=false}:${when:when=length('${ndlc}')>0:inner= [${ndlc}]} ${message} ${exception:format=tostring}" />
<!-- For Azure Log Stream -->
<target xsi:type="Trace"
name="azureLogStream"
rawWrite="true"
layout="X ${longdate:universalTime=true} [${level}] ${logger}.${callsite:className=false}:${when:when=length('${ndlc}')>0:inner= [${ndlc}]} ${message} ${exception:format=tostring}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="Microsoft.EntityFrameworkCore.*"
level="Information"
final="true" />
<logger name="Microsoft.*"
maxlevel="Information"
final="true" />
<logger name="Hangfire.*"
minlevel="Trace"
final="true" />
<!-- BlackHole without writeTo -->
<logger name="*"
minlevel="Trace"
writeTo="azureLogStream,allfile" />
<logger name="*"
minlevel="Trace"
writeTo="ConsoleTarget" />
</rules>
</nlog>
I've configured NLog to also write to a file and that is working.
From the streaming logs
2020-04-03 17:31:13.241 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://**BEEP**/
2020-04-03 17:31:13.242 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 6.3456ms 404
From the log file made by NLog
2020-04-03 16:02:59.9714 [Info] Namespace.Class.McWritey: [RequestId:800022a7-0000-e300-b63f-84710c7967bb RequestPath:/signalr/notifications TransportConnectionId:-qK6yuSdbcbr5PDDXJ-EBQ [Id: `ac5c3f80-456a-48b5-9122-6836f911e141`]] CloseFile File Closed by -qK6yuSdbcbr5PDDXJ-EBQ
2020-04-03 16:03:00.5218 [Info] Namespace.Class.McWritey: [RequestId:800022a7-0000-e300-b63f-84710c7967bb RequestPath:/signalr/notifications TransportConnectionId:-qK6yuSdbcbr5PDDXJ-EBQ] OnDisconnectedAsync Client Disconnected: -qK6yuSdbcbr5PDDXJ-EBQ
From the debug log when I attach VS to my published app
X 2020-04-03 17:39:19.1176 [Info] Namespace.Class.McWritey: [RequestId:8000161a-0000-f400-b63f-84710c7967bb RequestPath:/signalr/notifications TransportConnectionId:7ML3GmVzsm7mHPBl0sB-GA] OnConnectedAsync Client Connected: 7ML3GmVzsm7mHPBl0sB-GA