Note:
This documentation is for version5.2.0of the library.
Some samples may not work with other versions.
Installation
Install theGoogle.Cloud.Logging.NLogpackage from NuGet. Add it to
your project in the normal way (for example by right-clicking on the
project in Visual Studio and choosing "Manage NuGet Packages...").
Authentication
When running on Google Cloud, no action needs to be taken to authenticate.
Otherwise, the simplest way of authenticating your API calls is to
set up Application Default Credentials.
The credentials will automatically be used to authenticate. SeeSet up Application Default Credentialsfor more details.
Use this file to configure NLog and then log as normal:
// Configure nlog to use Google Stackdriver logging from the XML configuration file.
LogManager.Setup().LoadConfigurationFromFile("nlog.xml", optional: false);
// Acquire a logger for this class
Logger logger = LogManager.GetCurrentClassLogger();
// Log some information. This log entry will be sent to Google Stackdriver Logging.
logger.Info("An exciting log entry!");
// Flush buffered log entries before program exit; then shutdown the logger before program exit.
LogManager.Flush(TimeSpan.FromSeconds(15));
LogManager.Shutdown();
LogId- Name of log under the resource type (Default = "Default")
DisableResourceTypeDetection- Defaults to false. Setting this to true disables automatic resource type detection based on platform detection.
ResourceType- Resource type for logs. Must be one of the supported types listed in thecloud logging documentation. This is not needed if resource type detection is successful. If resource type detection fails or is disabled, and this is not set, the resource type in log entries is set to "global".
ContextProperty- Allows custom labels to be added to the log entries as extra metadata.
CredentialFile- Credentials using a JSON file, instead of using the application default credentials
CredentialJson- Credentials using raw JSON input, instead of using the application default credentials
IncludeCallSite- IncludeLogEntrySourceLocationextracted from NLog callsite. (This only includes the function name.)
IncludeCallSiteStackTrace- IncludeLogEntrySourceLocationextracted from NLog callsite. (This also includes file name and line number.)
IncludeEventProperties- Includes structured logging properties from NLogLogEventInfo.Properties.
IncludeMdlc- Include async thread context properties from NLog MappedDiagnosticsLogicalContext
SendJsonPayload- Use thejsonPayloadproperty in log entries instead oftextPayload. When this is enabled, properties are set within the payload instead of as custom labels
ServiceContextName- Configures the "service" in "serviceContext" when sending a JSON payload. (Defaults to theAppDomain.FriendlyNameof the running code.)
ServiceContextVersion- Configures the "version" in "serviceContext" when sending a JSON payload. (Defaults to theFileVersionof the entry assembly.)
EnableJsonLayout- Uses NLog's native JSON layout to format JSON payloads, completely replacing the default layout.
JsonConverterorJsonConverterTypeName/JsonConverterMethodName- Configures a custom conversion for individual values within a JSON payload.
SpanId- The span ID within the trace associated with the log entry. Trace API v2 uses a 16-character hexadecimal encoding of an 8-byte array, ex:000000000000004a. SeeGoogle Logging V2 LogEntry reference docsfor more information.
EnablingEnableJsonLayoutwill override the standard JSON format, and instead depend on the output from NLog JsonLayout. This will completely replace the JSON payload, but has a performance penalty because of additional parsing.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003e\u003ccode\u003eGoogle.Cloud.Logging.NLog\u003c/code\u003e is a .NET client library that integrates Google Stackdriver Logging with NLog, allowing developers to send log entries to Google Cloud.\u003c/p\u003e\n"],["\u003cp\u003eThe library supports easy authentication when running on Google Cloud or by setting up Application Default Credentials, and no authentication steps are needed when running on the cloud.\u003c/p\u003e\n"],["\u003cp\u003eConfiguration involves creating an \u003ccode\u003enlog.xml\u003c/code\u003e file where you can specify the Google Cloud Project ID, log ID, and other settings to control how logs are sent to Google Stackdriver Logging.\u003c/p\u003e\n"],["\u003cp\u003eIt provides various configuration options, such as \u003ccode\u003eSendJsonPayload\u003c/code\u003e and \u003ccode\u003eEnableJsonLayout\u003c/code\u003e, that allow for customization of log data sent to Stackdriver, including formatting as JSON payloads.\u003c/p\u003e\n"],["\u003cp\u003eThe latest version of this library is version 5.2.0, with previous versions also available for use, each with their own specific documentation.\u003c/p\u003e\n"]]],[],null,["Version latestkeyboard_arrow_down\n\n- [5.2.0 (latest)](/dotnet/docs/reference/Google.Cloud.Logging.NLog/latest)\n- [5.1.0](/dotnet/docs/reference/Google.Cloud.Logging.NLog/5.1.0)\n- [5.0.0](/dotnet/docs/reference/Google.Cloud.Logging.NLog/5.0.0)\n- [4.0.0](/dotnet/docs/reference/Google.Cloud.Logging.NLog/4.0.0)\n- [3.3.0](/dotnet/docs/reference/Google.Cloud.Logging.NLog/3.3.0) \n\nGoogle.Cloud.Logging.NLog\n=========================\n\n`Google.Cloud.Logging.NLog` is a .NET client library to integrate [Google Stackdriver\nLogging](https://cloud.google.com/logging/) with [NLog](http://nlog-project.org/).\n\nNote:\nThis documentation is for version `5.2.0` of the library.\nSome samples may not work with other versions.\n\nInstallation\n------------\n\nInstall the `Google.Cloud.Logging.NLog` package from NuGet. Add it to\nyour project in the normal way (for example by right-clicking on the\nproject in Visual Studio and choosing \"Manage NuGet Packages...\").\n\nAuthentication\n--------------\n\nWhen running on Google Cloud, no action needs to be taken to authenticate.\n\nOtherwise, the simplest way of authenticating your API calls is to\nset up Application Default Credentials.\nThe credentials will automatically be used to authenticate. See\n[Set up Application Default Credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc) for more details.\n\nGetting started\n---------------\n\nConsole app\n-----------\n\nCreate an `nlog` configuration file (`nlog.xml`): \n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\" ?\u003e\n \u003cnlog xmlns=\"http://www.nlog-project.org/schemas/NLog.xsd\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\u003e\n\n \u003cextensions\u003e\n \u003cadd assembly=\"Google.Cloud.Logging.NLog\" /\u003e\n \u003c/extensions\u003e\n \n \u003ctargets async=\"true\"\u003e\n \u003ctarget name=\"stackdriver\" xsi:type=\"GoogleStackdriver\" projectId=\"PROJECT_ID\" logId=\"LOG_ID\"\u003e\n \u003c!-- Potentially additional configuration --\u003e\n \u003c/target\u003e\n \u003ctarget name=\"console\" xsi:type=\"Console\" /\u003e\n \u003c/targets\u003e\n\n \u003crules\u003e\n \u003clogger name=\"*\" minlevel=\"Info\" writeTo=\"stackdriver\" /\u003e\n \u003clogger name=\"*\" minlevel=\"Info\" writeTo=\"console\" /\u003e\n \u003c/rules\u003e\n \n \u003c/nlog\u003e\n\nEdit the file replacing `PROJECT_ID` with your Google Cloud Project\nID, and `LOG_ID` with an identifier for your application.\n\nEnsure the file is copied to the output directory. For example, in\nyour project file you might include: \n\n \u003cNone Update=\"nlog.xml\" CopyToOutputDirectory=\"Always\" /\u003e\n\nUse this file to configure NLog and then log as normal: \n\n // Configure nlog to use Google Stackdriver logging from the XML configuration file.\n LogManager.Setup().LoadConfigurationFromFile(\"nlog.xml\", optional: false);\n\n // Acquire a logger for this class\n Logger logger = LogManager.GetCurrentClassLogger();\n // Log some information. This log entry will be sent to Google Stackdriver Logging.\n logger.Info(\"An exciting log entry!\");\n\n // Flush buffered log entries before program exit; then shutdown the logger before program exit.\n LogManager.Flush(TimeSpan.FromSeconds(15));\n LogManager.Shutdown();\n\nIf executing on [Google App Engine (GAE)](https://cloud.google.com/appengine/),\n[Google Kubernetes Engine (GKE)](https://cloud.google.com/kubernetes-engine/),\nor [Google Compute Engine (GCE)](https://cloud.google.com/compute/),\nthen the `projectId=\"PROJECT_ID\"` configuration setting can be omitted; it will be auto-detected from the platform at run-time.\n\nConfiguration options\n---------------------\n\n- `ProjectId` - Google Cloud project ID where logs will be sent to. Can be omitted if executing on on [Google App Engine (GAE)](https://cloud.google.com/appengine/) or [Google Compute Engine (GCE)](https://cloud.google.com/compute/)\n- `LogId` - Name of log under the resource type (Default = \"Default\")\n- `DisableResourceTypeDetection` - Defaults to false. Setting this to true disables automatic resource type detection based on platform detection.\n- `ResourceType` - Resource type for logs. Must be one of the supported types listed in the [cloud logging documentation](https://cloud.google.com/logging/docs/api/v2/resource-list). This is not needed if resource type detection is successful. If resource type detection fails or is disabled, and this is not set, the resource type in log entries is set to \"global\".\n- `ResourceLabel` - Used to specify extra labels as shown in the [Monitoring Resource Types](https://cloud.google.com/logging/docs/api/v2/resource-list) list.\n- `ContextProperty` - Allows custom labels to be added to the log entries as extra metadata.\n- `CredentialFile` - Credentials using a JSON file, instead of using the application default credentials\n- `CredentialJson` - Credentials using raw JSON input, instead of using the application default credentials\n- `IncludeCallSite` - Include `LogEntrySourceLocation` extracted from NLog callsite. (This only includes the function name.)\n- `IncludeCallSiteStackTrace` - Include `LogEntrySourceLocation` extracted from NLog callsite. (This also includes file name and line number.)\n- `IncludeEventProperties` - Includes structured logging properties from NLog `LogEventInfo.Properties`.\n- `IncludeMdlc` - Include async thread context properties from NLog MappedDiagnosticsLogicalContext\n- `SendJsonPayload` - Use the `jsonPayload` property in log entries instead of `textPayload`. When this is enabled, properties are set within the payload instead of as custom labels\n- `ServiceContextName` - Configures the \"service\" in \"serviceContext\" when sending a JSON payload. (Defaults to the `AppDomain.FriendlyName` of the running code.)\n- `ServiceContextVersion` - Configures the \"version\" in \"serviceContext\" when sending a JSON payload. (Defaults to the `FileVersion` of the entry assembly.)\n- `EnableJsonLayout` - Uses NLog's native JSON layout to format JSON payloads, completely replacing the default layout.\n- `JsonConverter` or `JsonConverterTypeName`/`JsonConverterMethodName` - Configures a custom conversion for individual values within a JSON payload.\n- `TraceId` - The trace ID to associate to log entries. Ex: `06796866738c859f2f19b7cfb3214824`. See [Google Logging V2 LogEntry reference docs](https://cloud.google.com/logging/docs/reference/v2/rpc/google.logging.v2#google.logging.v2.LogEntry) for more information.\n- `SpanId` - The span ID within the trace associated with the log entry. Trace API v2 uses a 16-character hexadecimal encoding of an 8-byte array, ex: `000000000000004a`. See [Google Logging V2 LogEntry reference docs](https://cloud.google.com/logging/docs/reference/v2/rpc/google.logging.v2#google.logging.v2.LogEntry) for more information.\n- `TraceSampled` - The sampling decision of the trace associated with the log entry. See [Google Logging V2 LogEntry reference docs](https://cloud.google.com/logging/docs/reference/v2/rpc/google.logging.v2#google.logging.v2.LogEntry) for more information.\n\nSending JsonPayload\n-------------------\n\nEnabling `SendJsonPayload` will capture structured logging properties in JSON format (instead of saving them as resource labels). \n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\" ?\u003e\n \u003cnlog xmlns=\"http://www.nlog-project.org/schemas/NLog.xsd\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\u003e\n\n \u003cextensions\u003e\n \u003cadd assembly=\"Google.Cloud.Logging.NLog\" /\u003e\n \u003c/extensions\u003e\n \n \u003ctargets async=\"true\"\u003e\n \u003ctarget name=\"stackdriver\" xsi:type=\"GoogleStackdriver\" projectId=\"PROJECT_ID\" logId=\"LOG_ID\" includeEventProperties=\"true\" sendJsonPayload=\"true\"\u003e\n \u003ccontextproperty name=\"exception\" layout=\"${exception:format=tostring}\" /\u003e \u003c!-- Optional and repeatable --\u003e\n \u003c/target\u003e\n \u003ctarget name=\"console\" xsi:type=\"Console\" /\u003e\n \u003c/targets\u003e\n\n \u003crules\u003e\n \u003clogger name=\"*\" minlevel=\"Info\" writeTo=\"stackdriver\" /\u003e\n \u003clogger name=\"*\" minlevel=\"Info\" writeTo=\"console\" /\u003e\n \u003c/rules\u003e\n \n \u003c/nlog\u003e\n\nCustom JsonPayload\n------------------\n\nEnabling `EnableJsonLayout` will override the standard JSON format, and instead depend on the output from NLog JsonLayout. This will completely replace the JSON payload, but has a performance penalty because of additional parsing. \n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\" ?\u003e\n \u003cnlog xmlns=\"http://www.nlog-project.org/schemas/NLog.xsd\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\u003e\n\n \u003cextensions\u003e\n \u003cadd assembly=\"Google.Cloud.Logging.NLog\" /\u003e\n \u003c/extensions\u003e\n \n \u003ctargets async=\"true\"\u003e\n \u003ctarget type=\"GoogleStackdriver\" name=\"stackdriver\"\n projectId=\"PROJECT_ID\" logId=\"LOG_ID\" resourceType=\"container\" sendJsonPayload=\"true\" enableJsonLayout=\"true\"\u003e\n \u003clayout xsi:type=\"JsonLayout\" includeAllProperties=\"true\"\u003e\n \u003cattribute name=\"someProp\" layout=\"prop\" /\u003e\n \u003cattribute name=\"nestedProp\" encode=\"false\"\u003e\n \u003clayout type='JsonLayout'\u003e\n \u003cattribute name='insideNested1' layout='hello nested1!' /\u003e\n \u003cattribute name='insideNested2' layout='hello nested2!' /\u003e\n \u003cattribute name=\"nestedNestedProp\" encode=\"false\"\u003e\n \u003clayout type='JsonLayout'\u003e\n \u003cattribute name='insideNestedNested1' layout='hello NestedNested1!' /\u003e\n \u003cattribute name='insideNestedNested2' layout='hello NestedNested2!' /\u003e\n \u003c/layout\u003e\n \u003c/attribute\u003e\n \u003c/layout\u003e\n \u003c/attribute\u003e\n \u003c/layout\u003e\n \u003c/target\u003e\n \u003ctarget name=\"console\" xsi:type=\"Console\" /\u003e\n \u003c/targets\u003e\n\n \u003crules\u003e\n \u003clogger name=\"*\" minlevel=\"Info\" writeTo=\"stackdriver\" /\u003e\n \u003clogger name=\"*\" minlevel=\"Info\" writeTo=\"console\" /\u003e\n \u003c/rules\u003e\n \n \u003c/nlog\u003e"]]