/

Global Properties

Global properties allow you to add consistent metadata that will be included with all subsequent logs automatically. This is useful for application-wide context like environment, version, or deployment information.

Adding Global Metadata

Use Log.AddMetaContext() to add metadata that will be included with all subsequent logs:

// Add global metadata
Log.AddMetaContext("AppVersion", "1.2.3");
Log.AddMetaContext("Environment", "Production");
Log.AddMetaContext("Server", Environment.MachineName);

// All subsequent logs will include this metadata
Log.Info("Application started"); // Includes AppVersion, Environment, Server
Log.Error("Database connection failed", null, ex); // Also includes the metadata

Common Global Metadata

Application Information

// Basic application context
Log.AddMetaContext("ApplicationName", "OrderService");
Log.AddMetaContext("Version", Assembly.GetExecutingAssembly().GetName().Version.ToString());
Log.AddMetaContext("Environment", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
Log.AddMetaContext("MachineName", Environment.MachineName);

Deployment Context

// Cloud deployment context
Log.AddMetaContext("DeploymentId", Environment.GetEnvironmentVariable("DEPLOYMENT_ID"));
Log.AddMetaContext("Region", Environment.GetEnvironmentVariable("AWS_REGION"));
Log.AddMetaContext("InstanceId", Environment.GetEnvironmentVariable("EC2_INSTANCE_ID"));
Log.AddMetaContext("ContainerId", Environment.GetEnvironmentVariable("HOSTNAME"));

Complex Objects and JSON

You can add any type of object, including complex objects that will be serialized as JSON:

// Simple values
Log.AddMetaContext("BuildNumber", 12345);
Log.AddMetaContext("IsDebugMode", true);

// Complex objects
var deploymentInfo = new
{
    Version = "1.2.3",
    Branch = "main",
    CommitHash = "abc123def",
    DeployedAt = DateTime.UtcNow
};
Log.AddMetaContext("Deployment", deploymentInfo);

// Arrays
Log.AddMetaContext("Features", new[] { "FeatureA", "FeatureB", "FeatureC" });

ASP.NET Core Integration

Set global metadata during application startup:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Set application-wide metadata
    Log.AddMetaContext("ApplicationName", "OrderService");
    Log.AddMetaContext("Environment", env.EnvironmentName);
    Log.AddMetaContext("Version", Assembly.GetExecutingAssembly().GetName().Version.ToString());
    
    // Configure middleware...
    app.UseRouting();
    app.UseEndpoints(endpoints => { /* ... */ });
}

Configuration-Based Metadata

Load metadata from configuration:

{
  "Logging": {
    "GlobalMetadata": {
      "ApplicationName": "OrderService",
      "Environment": "Production",
      "Region": "US-East-1",
      "Version": "2.1.0"
    }
  }
}
public void ConfigureServices(IServiceCollection services)
{
    // Load global metadata from configuration
    var globalMetadata = Configuration.GetSection("Logging:GlobalMetadata");
    foreach (var item in globalMetadata.GetChildren())
    {
        Log.AddMetaContext(item.Key, item.Value);
    }
}

Best Practices

Best Practices

  • Set global metadata during application startup, not runtime
  • Use consistent naming conventions (PascalCase recommended)
  • Include useful debugging context like version, environment, deployment info
  • Avoid sensitive data like passwords or tokens

Next Steps

Previous
Duplicate Suppression