This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"DOTNET_ENVIRONMENT": "Development", | |
"Logging": { | |
"LogLevel": { | |
"Default": "Warning", | |
"Microsoft": "Information", | |
"Microsoft.AspNetCore": "Warning" | |
} | |
}, | |
"ConnectionStrings": { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void UseElasticApm(this IApplicationBuilder app, IConfiguration configuration) | |
{ | |
app.UseAllElasticApm(configuration); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ElasticsearchExtensions | |
{ | |
public static void AddElasticsearch(this IServiceCollection services, IConfiguration configuration) | |
{ | |
var settings = new ConnectionSettings(new Uri(configuration["ElasticsearchSettings:uri"])); | |
var defaultIndex = configuration["ElasticsearchSettings:defaultIndex"]; | |
if (!string.IsNullOrEmpty(defaultIndex)) | |
settings = settings.DefaultIndex(defaultIndex); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SerilogExtensions | |
{ | |
public static WebApplicationBuilder AddSerilog(this WebApplicationBuilder builder, IConfiguration configuration, string applicationName) | |
{ | |
Log.Logger = new LoggerConfiguration() | |
.ReadFrom.Configuration(configuration) | |
.Enrich.FromLogContext() | |
.Enrich.WithMachineName() | |
.Enrich.WithEnvironmentUserName() | |
.Enrich.WithExceptionDetails() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = WebApplication.CreateBuilder(args); | |
builder.AddSerilog(builder.Configuration, "API Elastic APM"); | |
Log.Information("Starting API"); | |
builder.Services.AddApiConfiguration(); | |
builder.Services.AddElasticsearch(builder.Configuration); | |
builder.Services.AddSqlDatabase(builder.Configuration); | |
builder.Services.AddSwagger(builder.Configuration); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
var builder = WebApplication.CreateBuilder(args); | |
builder.AddSerilogApi(builder.Configuration); | |
builder.Services.AddControllers(); | |
builder.Services.AddEndpointsApiExplorer(); | |
builder.Services.AddExceptionHandler<ErrorHandlingMiddleware>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ErrorHandlingMiddleware : Microsoft.AspNetCore.Diagnostics.IExceptionHandler | |
{ | |
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken) | |
{ | |
using var property = LogContext.PushProperty("UserName", httpContext.User?.Identity?.Name ?? "anônimo"); | |
Log.Error(exception, "Error"); | |
var result = JsonSerializer.Serialize(new { error = exception?.Message }); | |
httpContext.Response.ContentType = "application/json"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
builder.Logging.ClearProviders(); | |
builder.Host.UseSerilog(Log.Logger, true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Log.Logger = new LoggerConfiguration() | |
.Enrich.FromLogContext() | |
.Enrich.WithExceptionDetails() | |
.Enrich.WithCorrelationId() | |
.Enrich.WithCorrelationIdHeader() | |
.WriteTo.Async(wt => wt.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Properties:j}{NewLine}{Exception}", restrictedToMinimumLevel: logLevel)) | |
.CreateLogger(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Log.Logger = new LoggerConfiguration() | |
.Enrich.FromLogContext() | |
.WriteTo.Async(writeTo => writeTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(configuration["ElasticsearchSettings:uri"])) | |
{ | |
CustomFormatter = new EcsTextFormatter(), | |
AutoRegisterTemplate = true, | |
IndexFormat = "indexlogs", | |
ModifyConnectionSettings = x => x.BasicAuthentication(configuration["ElasticsearchSettings:username"], configuration["ElasticsearchSettings:password"]) | |
})) | |
.CreateLogger(); |
NewerOlder