Skip to content

danielcandea/Nodinite.Serilog.ServiceBusSink

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Nodinite Logo

Nodinite.Serilog.ServiceBusSink

NuGet Version

A Serilog sink that writes log events to a Microsoft Azure Service Bus Queue.

This project is built with .NET Standard 2.0.

Get Started

Install Nodinite.Serilog.ServiceBusSink Nuget Package

Start by installing the NuGet package Nodinite.Serilog.Sink.Core.

Install-Package Nodinite.Serilog.ServiceBusSink

Configuration

Nodinite requires some settings to be configured in order for events to be logged. Below you can see all settings that need to be configured.

Field Example Value Comment
LogAgentValueId 503 Who (Log Agents) sent the data
EndPointName "Nodinite.Serilog.ServiceBusSink.Tests" Name of Endpoint transport
EndPointUri "Nodinite.Serilog.ServiceBusSink.Tests.Serilog" URI for Endpoint transport
EndPointDirection 0 Direction for Endpoint transport
EndPointTypeId 0 Type of Endpoint transport
OriginalMessageTypeName "Serilog.LogEvent" Message Type Name
ProcessingUser "Nodinite" Log Identity
ProcessName "My customer import process" Name of process
ProcessingMachineName "localhost" Name of server where log event originated
ProcessingModuleName "INT101-HelloHappyCustomers-Application" Name of module
ProcessingModuleType "FileImport" Type of module, exe, dll, service

Using code

Besides Serilog, the following nuget packages need to be installed

Using the following code below you can start logging events to Nodinite.

var connectionString = "{Your ServiceBus Connection String";
var queueName = "{Your ServiceBus Queue Name}";

var settings = new NodiniteLogEventSettings()
{
    LogAgentValueId = 503,
    EndPointDirection = 0,
    EndPointTypeId = 0,
    EndPointUri = "Nodinite.Serilog.ServiceBusSink.Tests.Serilog",
    EndPointName = "Nodinite.Serilog.ServiceBusSink.Tests",
    ProcessingUser = "NODINITE",
    ProcessName = "Nodinite.Serilog.ServiceBusSink.Tests",
    ProcessingMachineName = "NODINITE-DEV",
    ProcessingModuleName = "DOTNETCORE.TESTS",
    ProcessingModuleType = "DOTNETCORE.TESTPROJECT"
};

ILogger log = new LoggerConfiguration()
    .WriteTo.NodiniteServiceBusSink(connectionString, queueName, settings)
    .CreateLogger()
    .ForContext("ApplicationInterchangeId", $"CustomId-{Guid.NewGuid().ToString()}")
    .ForContext("CustomerId", 12)
    .ForContext("Body", JsonConvert.SerializeObject(new { Id = 1 }))
    .ForContext("OriginalMessageType", "TestMessage#1.0");

Using Appsettings.json (Preferred)

Besides Serilog, the following nuget packages need to be installed

Using the following code to initialize the logger in your application:

var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

Logger log = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .CreateLogger();

And putting the following into your appsettings.json:

{
  "Serilog": {
    "Using": [ "Nodinite.Serilog.ServiceBusSink" ],
    "WriteTo": [
      {
        "Name": "NodiniteServiceBusSink",
        "Args": {
          "ConnectionString": "",
          "QueueName":  "",
          "Settings": {
            "LogAgentValueId": 503,
            "EndPointName": "Nodinite.Serilog.ServiceBusSink.Tests",
            "EndPointUri": "Nodinite.Serilog.ServiceBusSink.Tests.Serilog",
            "EndPointDirection": 0,
            "EndPointTypeId": 0,
            "OriginalMessageTypeName": "Serilog.LogEvent",
            "ProcessingUser": "NODINITE",
            "ProcessName": "Nodinite.Serilog.ServiceBusSink.Tests",
            "ProcessingMachineName": "NODINITE-DEV",
            "ProcessingModuleName": "DOTNETCORE.TESTS",
            "ProcessingModuleType": "DOTNETCORE.TESTPROJECT"
          }
        }
      }
    ]
  }
}

Logging Context Properties

ILogger log = new LoggerConfiguration()
    .WriteTo.NodiniteServiceBusSink(connectionString, queueName, settings)
    .CreateLogger()
    .ForContext("CorrelationId", Guid.NewGuid())
    .ForContext("CustomerId", 12);

log.Information("Customer '12' has been imported.");

The Serilog sink will automatically loop over all context properties you have defined in your code and log them as part of your event to Nodinite.

Example:

nodinite.serilog.sink.core.context.properties

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages