Skip to content

Latest commit

 

History

History
126 lines (105 loc) · 2.68 KB

README.md

File metadata and controls

126 lines (105 loc) · 2.68 KB

ErrLogIO.AspNetCore NuGet

ASP.NET Core library for ErrLog.IO

Installation

.NET CLI

dotnet add package ErrLogIO.AspNetCore

Package Manager

Install-Package ErrLogIO.AspNetCore

Configuration

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddErrLogIO("API-KEY");
    }
}
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddErrLogIO(options =>
        {
            options.ApiKey = "API-KEY";
            options.AppName = "MyApp";
            options.Language = Language.CSharp;
            options.KeysToExclude = new [] {"password"};
            options.HideAllRequestValues = true;
        });
    }
}

appsettings.json

{
  "ErrLogIO": {
    "ApiKey": "API_KEY"
  }
}
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<ErrLogIOOptions>(
            Configuration.GetSection("ErrLogIO"));
        services.AddErrLogIO();
    }
}

Logging exceptions

To log every uncaught exception to ErrLog.IO, call UseErrLogIO in the Configure method. Please make sure to call the UseErrLogIO after installation of other pieces of middleware handling exceptions and auth, but before any calls to UseEndpoints, UseMvc etc.

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseProblemDetails();
        app.UseErrLogIO();
        
        ...
        ...
        ...
        
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

Dependency injection

public class StorageController : ControllerBase
{
    private readonly IStorageService _storageService;
    private readonly ErrLogIOService _errLogIO;

    public StorageController(IStorageService storageService, ErrLogIOService errLogIO)
    {
        _storageService = storageService;
        _errLogIO = errLogIO;
    }

    [HttpPost("upload")]
    public async Task<IActionResult> UploadFileAsync(CancellationToken ct)
    {
        try
        {
            var stream = Request.BodyReader.AsStream();
            await _storageService.UploadFileAsync(stream, ct);

            return Ok();
        }
        catch (Exception ex)
        {
            await _errLogIO.LogExceptionAsync(
                ex, Request.HttpContext, cancellationToken: ct);

            return BadRequest(ex.Message);
        }
    }
}