diff --git a/apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog/GoogleStackdriverTarget.cs b/apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog/GoogleStackdriverTarget.cs
index 791e6183953f..6b6004d6002f 100644
--- a/apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog/GoogleStackdriverTarget.cs
+++ b/apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog/GoogleStackdriverTarget.cs
@@ -473,6 +473,23 @@ private LogEntry BuildLogEntry(LogEventInfo loggingEvent)
{
var jsonStruct = new Struct();
jsonStruct.Fields.Add("message", Value.ForString(RenderLogEvent(Layout, loggingEvent)));
+ if (ServiceContextName != null)
+ {
+ var serviceName = RenderLogEvent(ServiceContextName, loggingEvent);
+ if (!string.IsNullOrEmpty(serviceName))
+ {
+ // Include ServiceContext to allow errors to be automatically forwarded
+ var serviceVersion = RenderLogEvent(ServiceContextVersion, loggingEvent);
+ if (string.IsNullOrEmpty(serviceVersion))
+ serviceVersion = "0.0.0.0";
+
+ var serviceContext = new Struct();
+ jsonStruct.Fields.Add("serviceContext", Value.ForStruct(serviceContext));
+ serviceContext.Fields.Add("service", Value.ForString(serviceName));
+ serviceContext.Fields.Add("version", Value.ForString(serviceVersion));
+ }
+ }
+
var propertiesStruct = new Struct();
jsonStruct.Fields.Add("properties", Value.ForStruct(propertiesStruct));
diff --git a/apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog/GoogleStackdriverTarget_Configuration.cs b/apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog/GoogleStackdriverTarget_Configuration.cs
index 5d74905e6900..808257e393d6 100644
--- a/apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog/GoogleStackdriverTarget_Configuration.cs
+++ b/apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog/GoogleStackdriverTarget_Configuration.cs
@@ -51,6 +51,16 @@ public partial class GoogleStackdriverTarget
///
public int TaskPendingLimit { get; set; } = 5;
+ ///
+ /// Configures "service" for the "serviceContext" when = true
+ ///
+ public Layout ServiceContextName { get; set; } = "${appdomain:cached=true:format=\\{1\\}}";
+
+ ///
+ /// Configures "version" for the "serviceContext" when = true
+ ///
+ public Layout ServiceContextVersion { get; set; } = "${assembly-version:cached=true:type=File}";
+
///
/// The resource type of log entries.
/// Default value depends on the detected platform. See the remarks section for details.