97 lines
3.5 KiB
C#
97 lines
3.5 KiB
C#
using OpenTelemetry.Resources;
|
||
using OpenTelemetry.Trace;
|
||
using Serilog;
|
||
using Serilog.Events;
|
||
using Serilog.Sinks.OpenTelemetry;
|
||
|
||
namespace Presentation
|
||
{
|
||
public class Program
|
||
{
|
||
public static void Main(string[] args)
|
||
{
|
||
// Configura Serilog como logger global antes de crear el builder
|
||
Log.Logger = new LoggerConfiguration()
|
||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||
.Enrich.FromLogContext()
|
||
.WriteTo.Console()
|
||
.WriteTo.OpenTelemetry(options =>
|
||
{
|
||
options.Endpoint = "http://localhost:4317"; // OTLP endpoint
|
||
options.Protocol = OtlpProtocol.Grpc;
|
||
options.ResourceAttributes = new Dictionary<string, object>
|
||
{
|
||
["service.name"] = "mmorales.photo-backend"
|
||
};
|
||
})
|
||
.CreateLogger();
|
||
|
||
try
|
||
{
|
||
Log.Information("Starting up");
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
builder.Host.UseSerilog(); // Usa Serilog como proveedor de logs por defecto
|
||
|
||
builder.Services.AddProblemDetails(options =>
|
||
options.CustomizeProblemDetails =
|
||
ctx => ctx.ProblemDetails.Extensions.Add("traceId", ctx.HttpContext.TraceIdentifier)
|
||
);
|
||
builder.Services.AddOpenTelemetry()
|
||
.WithTracing(tracerProviderBuilder =>
|
||
{
|
||
tracerProviderBuilder
|
||
.SetResourceBuilder(ResourceBuilder.CreateDefault()
|
||
.AddService(builder.Environment.ApplicationName))
|
||
.AddAspNetCoreInstrumentation() // Traza todas las peticiones HTTP entrantes
|
||
.AddHttpClientInstrumentation() // Traza las llamadas HttpClient salientes
|
||
.AddOtlpExporter(opt =>
|
||
{
|
||
opt.Endpoint = new Uri("http://localhost:4317"); // Direcci<63>n del colector OTel
|
||
})
|
||
.AddConsoleExporter(); // Exporta trazas tambi<62>n a consola para desarrollo
|
||
});
|
||
// Add services to the container.
|
||
|
||
builder.Services.AddControllers();
|
||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||
builder.Services.AddOpenApi();
|
||
|
||
var app = builder.Build();
|
||
if (!app.Environment.IsDevelopment())
|
||
{
|
||
app.UseExceptionHandler();
|
||
app.UseHsts();
|
||
}
|
||
app.UseStatusCodePages();
|
||
|
||
app.UseAuthentication(); // Habilita autenticaci<63>n
|
||
app.UseAuthorization(); // Habilita autorizaci<63>n
|
||
|
||
// Configure the HTTP request pipeline.
|
||
if (app.Environment.IsDevelopment())
|
||
{
|
||
app.MapOpenApi();
|
||
}
|
||
|
||
app.UseHttpsRedirection();
|
||
|
||
app.UseAuthorization();
|
||
|
||
|
||
app.MapControllers();
|
||
|
||
app.Run();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Fatal(ex, "Application start-up failed");
|
||
throw;
|
||
}
|
||
finally
|
||
{
|
||
Log.CloseAndFlush();
|
||
}
|
||
}
|
||
}
|
||
}
|