SLogger 1.0.2

dotnet add package SLogger --version 1.0.2
                    
NuGet\Install-Package SLogger -Version 1.0.2
                    
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="SLogger" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SLogger" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="SLogger" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SLogger --version 1.0.2
                    
#r "nuget: SLogger, 1.0.2"
                    
#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.
#:package SLogger@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SLogger&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=SLogger&version=1.0.2
                    
Install as a Cake Tool

SLogger (a SeriLog implementation)

This is an implemention of SeriLog using DotNet Core and tweaked for EF Core

  • version dotnet core 3.1

SETUP

Installation of NuGet package

  • at this use a the custom store where the nuget package is located

Setup in the startup.cs class

  • Declare the following variables and fill in the product name and the layer
    private string ApplicationName = "ToDos";
    private string ApplicationLayer = "Core API";
  • Put following code the method ConfigureServices(IServiceCollection services)
    // section fwk slogger: Performance logging
    services.AddMvc(options =>
            {
                options.Filters.Add(new TrackPerformanceFilter(ApplicationName, ApplicationLayer));
                options.EnableEndpointRouting = false;
            }
        );
    // section fwk slogger: Performance logging
  • Put following code the method Configure(IApplicationBuilder app, IWebHostEnvironment env)
    services.AddDbContext<ToDoDbContext>(options =>
    {
        bool enableDbContextLog = bool.Parse(Environment.GetEnvironmentVariable("ENABLE_DBCONTEXT") ?? "false");
        if (enableDbContextLog) options.UseLoggerFactory(Logger.GetILoggerFactory());
        if (enableDbContextLog) options.EnableSensitiveDataLogging();
        options.UseSqlServer(Environment.GetEnvironmentVariable("APPLICATION_DATABASE_CONNECTION"));
    });
// section fwk slogger: Global exception handeling
app.UseExceptionHandler(eApp =>
{
    eApp.Run(async context =>
    {
        context.Response.StatusCode = 500;
        context.Response.ContentType = "application/json";

        var errorCtx = context.Features.Get<IExceptionHandlerFeature>();
        if (errorCtx != null)
        {
            var ex = errorCtx.Error;
            WebLogger.LogWebError(ApplicationName, ApplicationLayer, ex, context);

            var errorId = Activity.Current?.Id ?? context.TraceIdentifier;
            String messageValue = "";
            if (env.IsDevelopment())
            {
                messageValue = errorCtx.Error.Message + " " + errorCtx.Error.StackTrace;
            }
            else
            {
                messageValue = "An Server Error Occured, " +
                    "please contact your system administrator " +
                    "with following the error code";
            }
            var jsonResponse = JsonConvert.SerializeObject(new CustomErrorResponse
            {
                ErrorId = errorId,
                Message = messageValue
            });
            await context.Response.WriteAsync(jsonResponse, Encoding.UTF8);
            // For MVC Naveigate to MVC Error Page
            // app.UseExceptionHandler("/Home/Error");

        }
    });
});
// section fwk slogger: Global exception handeling
  • db context logging, this visualizes the command that are executed against the database

  • Put following code the the main of program.cs and add the .UseSeriLog() to the host, You also need to add package Serilog.AspNetCore to make this to work

 Log.Logger = SLogger.Logger.GetLoggerHostConfig("_<productname>");

 Host....
    .UseSerilog();


## Enviroment variables to configure

--LOG Destinations--

- ENABLE_LOG_FILE (default : true)
- ENABLE_LOG_MSSQL (default : false)
- ENABLE_LOG_ELASTIC (default : false)

**File Logger**

- LOGFILE_PERF (default : performance.log)
- LOGFILE_USAGE (default : usage.log)
- LOGFILE_ERROR (default : error.log)
- LOGFILE_DIAG (default : diagnostics.log)
- LOGFILE_DBCONTEXT (default : dbcontext.log)
- LOGFILE_HOST (default : host.log)

**MSSQL Logger**

- "LOGDB_CONNECTION": "Server=localhost;Initial Catalog=logging;User Id=sa;Password=[password]",

**ElasticSearch Logger**

- ELASTIC_URL

**Logging Types**
- ENABLE_HOST (default : true)
- ENABLE_DIAG (default : false)
- ENABLE_USAGE (default : false)
- ENABLE_PERF (default : false)
- ENABLE_DBCONTEXT (default : false)

Environement Examples

// Destinations
"ENABLE_LOG_FILE": "true",
"ENABLE_LOG_MSSQL": "true",
"ENABLE_LOG_ELASTIC": "true",

// Types
"ENABLE_DIAG": "true",
"ENABLE_USAGE": "true",
"ENABLE_PERF": "true",
"ENABLE_DBCONTEXT": "true",

// logfilenames
"LOGFILE_DIAG": "performance.log",
"LOGFILE_USAGE": "usage.log",
"LOGFILE_ERROR": "error.log",
"LOGFILE_PERF": "diagnostics.log",

// DB Connectionstring
"LOGDB_CONNECTION": "Server=localhost;Initial Catalog=logging;User Id=sa;Password=[password]",

// Elastic search url
"ELASTICSEARCH_URL" : "http://localhost:9200",

Hosting on IIS

In iis you can configure enviroment variables using the iis manager or add the variables direct in the web.config

    <environmentVariables>
        <environmentVariable name="ENABLE_DIAG" value="true" />
        <environmentVariable name="ENABLE_USAGE" value="true" />
        <environmentVariable name="ENABLE_PERF" value="true" />
        <environmentVariable name="ENABLE_DBCONTEXT" value="true" />
        <environmentVariable name="ENABLE_LOG_FILE" value="true" />
        <environmentVariable name="ENABLE_LOG_MSSQL" value="true" />
        <environmentVariable name="LOGDB_CONNECTION" value="Server=localhost;Initial Catalog=identityserver4-logging;User Id=sa;Password=[03879e0aa70850ed0357ffX74d11fe90]" />
        <environmentVariable name="ENABLE_LOG_ELASTIC" value="false" />
        <environmentVariable name="ELASTICSEARCH_URL" value="http://localhost:9200" />
    </environmentVariables>

How to use

Usage Tracking

With this data annotation you can tag the webmethods for the usage logging


[TrackUsage("ToDos", "API Todo", "Retrieve Todo List")]
[TrackUsage(ApplicationName, ApplicationLayer, "Retrieve Todo List")]

Diagnostics Logging

Performance Tracking

automaticly used what setup is done, his can be turned on and off in the enviroment variables

Error Logging

automaticly used what setup is done, This can be turned on and off in the enviroment variables
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
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.0.2 679 3/10/2020