---
title: "Advanced Configuration Example"
description: "Review an advanced example of configuration for NLog."
url: https://docs.sentry.io/platforms/dotnet/guides/nlog/configuration/advanced-configuration-example/
---

# Advanced Configuration Example for NLog

##### NLog Layouts

For more information on how to dynamically set event data via `NLog.config`, see NLog's [layout renderer documentation](https://nlog-project.org/config/?tab=layout-renderers).

Call `SentrySdk.Init` to initialize the SDK first. SDK options, such as the environment and release, go here:

```csharp
SentrySdk.Init(options =>
{
    options.Dsn = "https://<key>@o<orgId>.ingest.sentry.io/<projectId>";
    options.Environment = "Development";
    options.Release = "my-project-name@2.3.12";
    options.AttachStacktrace = true;
    options.SendDefaultPii = true; // Send Personal Identifiable information like the username of the user logged in to the device
    options.ShutdownTimeout = TimeSpan.FromSeconds(5);
});
```

*Other available variations of the above snippet: fsharp*

Then configure the Sentry target in an `NLog.config` XML file:

```xml
<?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"
>
  <extensions>
    <add assembly="Sentry.NLog" />
  </extensions>

  <targets>
    <target
      xsi:type="Sentry"
      name="sentry"
      includeEventProperties="True"
      layout="${message}"
      breadcrumbLayout="${message}"
      minimumBreadcrumbLevel="Debug"
      ignoreEventsWithNoException="False"
      includeEventDataOnBreadcrumbs="True"
      includeEventPropertiesAsTags="True"
      minimumEventLevel="Error"
      minimumLogLevel="Info"
    >

      <!-- Optionally specify user properties via NLog
          (here using MappedDiagnosticsLogicalContext as an example) -->
      <user
        id="${mdlc:item=id}"
        username="${mdlc:item=username}"
        email="${mdlc:item=email}"
        ipAddress="${mdlc:item=ipAddress}"
      />

      <!--Optionally add any desired additional Tags that will be sent with every message -->
      <tag
        name="exception"
        layout="${exception:format=shorttype}"
        includeEmptyValue="false"
      />

      <!--Optionally add any desired additional Data that will be sent with every message -->
      <contextproperty
        name="threadid"
        layout="${threadid}"
        includeEmptyValue="true"
      />
    </target>
  </targets>

  <rules>
    <logger name="*" writeTo="sentry" />
  </rules>
</nlog>
```

Or configure the target in code:

```csharp
LogManager.Configuration.AddSentry(o =>
{
    o.Layout = "${message}";
    o.BreadcrumbLayout = "${logger}: ${message}"; // Optionally specify a separate format for breadcrumbs

    o.MinimumBreadcrumbLevel = LogLevel.Debug; // Debug and higher are stored as breadcrumbs (default is Info)
    o.MinimumEventLevel = LogLevel.Error; // Error and higher is sent as event (default is Error)
    o.MinimumLogLevel = LogLevel.Info; // Info and higher are sent as logs (default is Info)

    o.IncludeEventDataOnBreadcrumbs = true; // Optionally include event properties with breadcrumbs

    //Optionally specify user properties via NLog (here using MappedDiagnosticsLogicalContext as an example)
    o.User = new SentryNLogUser
    {
        Id = "${mdlc:item=id}",
        Username = "${mdlc:item=username}",
        Email = "${mdlc:item=email}",
        IpAddress = "${mdlc:item=ipAddress}",
        Other =
        {
            new TargetPropertyWithContext("mood", "joyous")
        },
    };

    o.AddTag("logger", "${logger}");  // Send the logger name as a tag

    // Other configuration
});
```

*Other available variations of the above snippet: fsharp*

---

Find other pages: [Sitemap](https://docs.sentry.io/sitemap.md) lists every page with a one-line summary, and [llms.txt](https://docs.sentry.io/llms.txt) gives an overview with guidance for agents.
