NLog Target for Microsoft Visual Studio App Center with Azure
-
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 Include="NLog.Extensions.Logging" Version="5.*" />
-
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;
-
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();
- AppSecret - Appsecret for starting AppCenter if needed (optional)
- UserId - Application UserId to register in AppCenter (optional)
- LogUrl - Base URL (scheme + authority + port only) to the AppCenter-backend (optional)
- CountryCode - Two-letter ISO country code to send to the AppCenter-backend (optional)
- DataResidencyRegion - Country Code or other identifier of data residency region (optional)
- ReportExceptionAsCrash - Report all exceptions as crashes to AppCenter (default=false)
- IncludeEventProperties - Include LogEvent properties in AppCenter properties (default=true)
- IncludeScopeProperties - Include MappedDiagnosticsLogicalContext (MLDC) that can be provided with MEL BeginScope (default=false)
<nlog throwConfigExceptions="true">
<extensions>
<add assembly="NLog.Targets.AppCenter"/>
</extensions>
<targets>
<target name="appcenter" type="appcenter" layout="${message}" reportExceptionAsCrash="true">
<contextproperty name="logger" layout="${logger}" />
<contextproperty name="loglevel" layout="${level}" />
<contextproperty name="threadid" layout="${threadid}" />
</target>
</targets>
<rules>
<logger name="*" minLevel="Info" writeTo="appcenter" />
</rules>
</nlog>