ASP.NET Core library for ErrLog.IO
.NET CLI
dotnet add package ErrLogIO.AspNetCore
Package Manager
Install-Package ErrLogIO.AspNetCore
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;
});
}
}
{
"ErrLogIO": {
"ApiKey": "API_KEY"
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.Configure<ErrLogIOOptions>(
Configuration.GetSection("ErrLogIO"));
services.AddErrLogIO();
}
}
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();
});
}
}
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);
}
}
}