SerilogEcsLogging 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package SerilogEcsLogging --version 1.0.3                
NuGet\Install-Package SerilogEcsLogging -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SerilogEcsLogging" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SerilogEcsLogging --version 1.0.3                
#r "nuget: SerilogEcsLogging, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SerilogEcsLogging as a Cake Addin
#addin nuget:?package=SerilogEcsLogging&version=1.0.3

// Install SerilogEcsLogging as a Cake Tool
#tool nuget:?package=SerilogEcsLogging&version=1.0.3                

SerilogEcsLogging

Library adding extensions methods to log ECS-structured events using serilog

Usage

Add SerilogEcsLogging nuget package to your project:

dotnet add package SerilogEcsLogging

Program.cs:

var builder = WebApplication.CreateBuilder(args);
// ...
builder.Host.UseSerilogEvents(logEcsEvents: true);
// ...
var app = builder.Build();
// ...
app.Run();

Create event class for each event type:

public class ServiceStartupEvent : EventBase
{
    private const string ActionName = "ServiceStartup";
    
    public ServiceStartupEvent(bool success = false)
        : base(ActionName, success, null /*Captured implicitly by TimedEvent*/, success ? "Service started successfully" : "Service startup failed")
    {
    }
}
public class DatabaseConnectionEvent : EventBase
{
    private const string ActionName = "DatabaseConnection";
    
    public class DatabaseConnectionDetails
    {
        public string ConnectionString { get; }
        
        public string SchemaVersion { get; }

        public DatabaseConnectionDetails(string connectionString, string schemaVersion)
        {
            ConnectionString = connectionString;
            SchemaVersion = schemaVersion;
        }
    }
    
    public DatabaseConnectionEvent() : base(ActionName, false, null /*Captured implicitly by TimedEvent*/, "Unable to establish connection to database")
    {
    }

    public DatabaseConnectionEvent(string connectionString, string schemaVersion)
        : base(ActionName, true, null /*Captured implicitly by TimedEvent*/, "Connected to database", new DatabaseConnectionDetails(connectionString, schemaVersion))
    {
    }
}

Trace events:

public async Task StartAsync(CancellationToken cancellationToken)
{
    using (var startupEvent = logger.BeginTimedEvent(new ServiceStartupEvent(), autoComplete: false))
    {
        try
        {
            // Perform app startup tests
            
            // Startup success
            startupEvent.Complete(new ServiceStartupEvent(true));
        }
        catch (Exception e)
        {
            // Startup failure
            startupEvent.Fail(e);
            throw;
        }
    }
using (var ev = logger.BeginTimedEvent(new DatabaseConnectionEvent(), autoComplete: false))
{
    try
    {
        // ...
        ev.Complete(new DatabaseConnectionEvent(dataAccess?.ConnectionString, dataAccess?.SchemaVersion));
    }
    catch (Exception e)
    {
        ev.Fail(e);
        throw;
    }
}

Traced ECS event:

{
  "@timestamp": "2023-02-10T08:55:22.6289711-06:00",
  "log.level": "Information",
  "message": "Connected to database",
  "tags": ["testApp"],
  "ecs": {
    "version": "1.5.0"
  },
  "event": {
    "kind": "event",
    "action": "DatabaseConnection",
    "outcome": "success",
    "severity": 4,
    "timezone": "Central Standard Time",
    "created": "2023-02-10T08:55:22.6289711-06:00",
    "start": "2023-02-10T08:55:22-06:00"
  },
  "host": {
    "name": "test-ec2"
  },
  "log": {
    "logger": "testApp.Startup.Startup",
    "original": null,
    "origin": {
      "file": {
        "name": "Startup.cs",
        "line": 37
      },
      "function": "StartAsync"
    }
  },
  "process": {
    "thread": {
      "id": 4
    },
    "pid": 348,
    "name": "testApp",
    "executable": "testApp"
  },
  "server": {
    "user": {
      "name": "test"
    }
  },
  "service": {
    "name": "testApp",
    "version": "1.0.0.0"
  },
  "Data": {
    "connection_string": "Server=localhost;Port=3306;Database=test_db;User ID=test_user",
    "schema_version": "1.0",
    "$type": "DatabaseConnectionDetails"
  }
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 182 2/6/2024
1.1.6 194 2/1/2024
1.1.5 196 12/21/2023
1.1.1 149 12/20/2023
1.0.8 185 12/15/2023
1.0.7 133 12/14/2023
1.0.6 192 11/14/2023
1.0.5 127 11/9/2023
1.0.4 161 11/1/2023
1.0.3 326 10/25/2023
1.0.0 926 3/16/2023