NLog target for Microsoft Visual Studio App Center with Azure
NLog Target for Microsoft Visual Studio App Center with Azure
⚠️ Microsoft AppCenter will be retired - https://learn.microsoft.com/appcenter/retirement
1) Install the NLog packages
Install-Package NLog.Targets.AppCenter
Install-Package NLog.Extensions.Logging
or in your csproj:
<PackageReference Include="NLog.Targets.AppCenter" Version="5.*" ></PackageReference>
<PackageReference Include="NLog.Extensions.Logging" Version="5.*" ></PackageReference>
2) Add NLog to the MauiApp
Update MauiProgram.cs
to include NLog as Logging Provider:
var builder = MauiApp.CreateBuilder();
// Add NLog for Logging
builder.Logging.ClearProviders();
builder.Logging.AddNLog();
If getting compiler errors with unknown methods, then update using
-section:
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
3) Load NLog configuration for logging
Add the NLog.config
-file into the Application-project as assembly-resource (Build Action
= embedded resource
), and load like this:
NLog.LogManager.Setup().RegisterAppCenter()
.LoadConfigurationFromAssemblyResource(typeof(App).Assembly);
Alternative setup NLog configuration using fluent-API:
var logger = NLog.LogManager.Setup().RegisterAppCenter()
.LoadConfiguration(c => c.ForLogger().FilterMinLevel(NLog.LogLevel.Debug).WriteToAppCenter())
.GetCurrentClassLogger();
<nlog throwConfigExceptions="true">
<extensions>
<add assembly="NLog.Targets.AppCenter"></add>
</extensions>
<targets>
<target name="appcenter" type="appcenter" layout="${message}" reportExceptionAsCrash="true">
<contextproperty name="logger" layout="${logger}" ></contextproperty>
<contextproperty name="loglevel" layout="${level}" ></contextproperty>
<contextproperty name="threadid" layout="${threadid}" ></contextproperty>
</target>
</targets>
<rules>
<logger name="*" minLevel="Info" writeTo="appcenter" ></logger>
</rules>
</nlog>