Skip to content

Logging Custom

Daniel edited this page Aug 16, 2020 · 9 revisions

Create and add a custom log service

To create and add your custom log service you need to know and implement a few base classes which are part of the framework. A new log service implementation always consists of three components (options, service, provider).

Create the log service options

The log service options are the interface between the framework and the user / developer. Here you can specify properties which can be set by the user and consumed by your log service.

Sample:

// Make sure to implement the LogOptions base class
public class CustomLogServiceOptions : LogOptions
{
    public string Property 
    {
        get => GetValue<string>();
        set => SetValue(value);
    }
}

Create the log service

Should contain the log message processing logic.

Sample:

// Make sure to implement the generic ScopedLogService base class
public class CustomLogService : ScopedLogService<CustomLogServiceOptions>
{
    public CustomLogService(string context) 
        : base(context)
    {
    }

    protected override void PublishMessage(LogMessage message)
    {
        // process the message
    }
}

Create the log service provider

Should contain the logic to correctly instantiate your custom log service.

Sample:

// Make sure to implement the ILogServiceProvider interface
public class CustomLogServiceProvider : ILogServiceProvider
{
    public IScopedLogService Create(string context)
    {
        return new CustomLogService(context);
    }
}

Register your log service

To use your custom log service you just need to add it to the factory.

Sample:

// Make sure to implement the generic ScopedLogService base class
class Program
{
    static void Main(string[] args)
    {
        LogServiceFactory.Instance.AddProvider<CustomLogServiceProvider>();

        var logger = LogServiceFactory.Instance.Create(nameof(Program));
        logger.Error("test");
    }
}