Skip to main content.

Site Map

Site Map

LOGGING SERVICE BRICK

Overview

The Logging Service Brick has been developed to provide logging for all microservices. It registers with Microsoft.Extensions.Logging and receives log messages generated by the system. These log messages are then persisted to storage for auditing.

Developers use this in their application code by injecting a Microsoft.Extensions.Logging.ILogger interface into their class. This interface allows writing fatal, error, warning, information or debug messages.

Requirements

  • Normal Startup Extensions for AddBrick, RegisterBrick and StartBrick with a storage provider
  • When creating the hostbuilder (Program.cs) and calling the ConfigureLogging() method to setup logging, add a call from ServiceBrick.Logging for loggingBuilder.AddServiceBrickLogger(); to register logging.
  • When the website configures middleware (Extensions/ApplicationBuilderExtensions.cs) with RegisterMiddleware(), add a call from ServiceBrick.Logging for applicationBuilder.UseMiddleware<LoggingMiddleware>(); to register the logging middleware.

Developer Notes

The ServiceBrick.Logging.CustomLogger class contains a static, in-memory ConcurrentQueue that all log messages are written to, named MessageQueue.

The LoggingMiddleware is responsible for adding HttpContext properties into a Dictionary which is scoped to the the current request. These properties are then serialized using the LogMessageDto.Properties property stored with the model. To change these properies, create a new class that inherits from LoggingMiddleware and override the GetStateObject() method.

Background Tasks

The ServiceBrick.Logging.LoggingWriteMessageTimer runs every second and checks if any log messages are available in the queue. If messages are available, it invokes the ServiceBrick.Logging.LoggingWriteMessageTask, which is responsible for pulling any messages from the queue and writing them to the configured storage provider.

Interfaces and Data Transfer Objects


using ServiceBrick.Logging.Api;

public interface ILogMessageApiService : IApiService<LogMessageDto> { }   
public interface ILogMessageApiClient : IApiClient<LogMessageDto>, ILogMessageApiService { }
public class LogMessageDto : DataTransferObject
{
    public virtual DateTimeOffset CreateDate { get; set; }
    public virtual string Application { get; set; }
    public virtual string Server { get; set; }
    public virtual string Category { get; set; }
    public virtual string UserStorageKey { get; set; }
    public virtual string Path { get; set; }
    public virtual string Level { get; set; }
    public virtual string Message { get; set; }
    public virtual string Exception { get; set; }
    public virtual string Properties { get; set; }
}

Application Settings


{
    "ServiceBrick": {
        "Logging": {
            "Client": {
                "ApiConfig": {
                    "ServiceUrl": "https://localhost:7000",
                    "TokenUrl": "https://localhost:7000/api/v1.0/Security/Authentication/AuthenticateUser",
                    "TokenType": "password",
                    "TokenClient": "email@servicebrick.com",
                    "TokenSecret": "mypassword"
                }
            },

            // Storage Providers
            "AzureDataTables":{
                "ConnectionString": ""
            },
            "EntityFrameworkCore":{
                "ConnectionString": ""
            },
            "MongoDb":{
                "ConnectionString": "",
                "DatabaseName": ""
            }
        }
    }
}

Reference