Skip to content

ASP.NET Core

Chad Ramos edited this page Mar 21, 2019 · 10 revisions

Global Exception Handling

Add PioneerLogsConfiguration object to your appsettings.json file.

{
  ...
  "PioneerLogsConfiguration": {
    "ApplicationName": "Pioneer Logs",
    "ApplicationLayer": "Pioneer.Logs.Samples.AspNetCore",
    "Errors": {
      "WriteToFile": false,
      "WriteToConsole": true
    },
    "Usage": {
      "WriteToFile": false,
      "WriteToConsole": true
    },
    "Performance": {
      "WriteToFile": false,
      "WriteToConsole": true
    },
    "Diagnostics": {
      "WriteToFile": true,
      "WriteToConsole": true
    }
  }
  ...
}

Add Pioneer Logs service in your Startup.cs file.

using Pioneer.Logs.Tubs.AspNetCore;
...

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddPioneerLogs(Configuration.GetSection("PioneerLogsConfiguration"));
    ...
}

forceWriteToFile

Default write targets are demonstrated above. You can override both console and file globally, per log category. If you need to overwrite on a log to log context, you can provide the forceWriteToFile flag to any category.

PioneerLogsTub.LogError("Oh NO!!!", forceWriteToFile: true);

Add Pioneer Logs middleware

Default

With this setup, your logs will use the File Serilog Sink.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    ...
    app.UsePioneerLogs();
    ...
}

Override default loggers

You can override all log writer Sinks, by providing a configuration option to the Middleware. A list of Sinks and how to configure them is available at Serilog Sinks.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    ...
    app.UsePioneerLogs(new PioneerLogsTubConfiguration {
        PerformanceLogger = new LoggerConfiguration()
            .WriteTo.File(path: @"logs\performance-override-name.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger(),
        UsageLogger = new LoggerConfiguration()
            .WriteTo.File(path: @"logs\usage-override-name.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger(),
        ErrorLogger = new LoggerConfiguration()
            .WriteTo.File(path: @"logs\error-override-name.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger(),
        DiagnosticLogger = new LoggerConfiguration()
            .WriteTo.File(path: @"logs\diagnostic-override-name.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger()
    });
    ...
}

Usage Tracking

You can supply a PioneerLogsTrackUsage attribute to any of your methods that you would like to track usage on.

[HttpGet]
[Route("api/exception")]
[PioneerLogsTrackUsage(Message = "Exception Get")]
public ActionResult<IEnumerable<string>> Get()
{
    throw new Exception("Force Exception");
}

Global Performance tracking

You can apply a the PioneerLogsPerformanceFilter filter global to track performance.

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddMvc(options => 
            options.Filters.Add(new PioneerLogsPerformanceFilter()))
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    ...
}

Manual Logging

public static void LogUsage(string message, HttpContext context = null, Dictionary<string, object> additionalInfo = null, bool forceWriteToFile = false);
public static void LogDiagnostic(string message,HttpContext context = null, Dictionary<string, object> additionalInfo = null, bool forceWriteToFile = false);
public static void LogError(Exception ex, HttpContext context = null, Dictionary<string, object> additionalInfo = null, bool forceWriteToFile = false);
public static void LogError(string message, HttpContext context = null, Dictionary<string, object> additionalInfo = null, bool forceWriteToFile = false);