fix: seq disponivel eventualmente. #12
@ -7,24 +7,139 @@ namespace YTExtractor.Logging.Configuration
|
||||
{
|
||||
public static class SerilogConfiguration
|
||||
{
|
||||
public static LoggerConfiguration SetLoggerConfiguration(this WebApplicationBuilder builder, LoggerConfiguration config, IServiceProvider services, IConfiguration configuration)
|
||||
public static LoggerConfiguration SetLoggerConfiguration(
|
||||
this WebApplicationBuilder builder,
|
||||
LoggerConfiguration config,
|
||||
IServiceProvider services,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
var workspace = configuration["Serilog:Properties:Workspace"];
|
||||
var seqServer = configuration.GetValue<string>("Serilog:WriteTo:2:Args:serverUrl"); ;
|
||||
var seqServer = configuration.GetValue<string>("Serilog:WriteTo:2:Args:serverUrl");
|
||||
|
||||
config
|
||||
.ReadFrom.Configuration(configuration)
|
||||
.ReadFrom.Services(services)
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithEnvironmentName()
|
||||
//.Enrich.WithMachineName()
|
||||
.Enrich.WithProperty("Application", "SumaTube")
|
||||
.Enrich.WithProperty("Workspace", workspace)
|
||||
.WriteTo.Seq(seqServer)
|
||||
;
|
||||
try
|
||||
{
|
||||
// Usa a configuração do appsettings.json (mais elegante)
|
||||
config
|
||||
.ReadFrom.Configuration(configuration)
|
||||
.ReadFrom.Services(services)
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithEnvironmentName()
|
||||
//.Enrich.WithMachineName()
|
||||
.Enrich.WithProperty("Application", "YTExtractor")
|
||||
.Enrich.WithProperty("Workspace", workspace);
|
||||
|
||||
return config;
|
||||
// Testa a conectividade com o Seq se estiver configurado
|
||||
if (!string.IsNullOrEmpty(seqServer))
|
||||
{
|
||||
TestSeqConnectivity(seqServer);
|
||||
}
|
||||
|
||||
Console.WriteLine("✅ Serilog configurado com sucesso a partir do appsettings.json");
|
||||
if (!string.IsNullOrEmpty(seqServer))
|
||||
{
|
||||
Console.WriteLine($"✅ Seq configurado: {seqServer}");
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"❌ ERRO ao configurar Serilog: {ex.Message}");
|
||||
Console.WriteLine("🔄 Tentando configuração de fallback...");
|
||||
|
||||
// Configuração de fallback se a configuração principal falhar
|
||||
return CreateFallbackConfiguration(config, workspace, seqServer);
|
||||
}
|
||||
}
|
||||
|
||||
private static LoggerConfiguration CreateFallbackConfiguration(
|
||||
LoggerConfiguration config,
|
||||
string workspace,
|
||||
string seqServer)
|
||||
{
|
||||
try
|
||||
{
|
||||
config
|
||||
.MinimumLevel.Information()
|
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithEnvironmentName()
|
||||
.Enrich.WithProperty("Application", "YTExtractor")
|
||||
.Enrich.WithProperty("Workspace", workspace ?? "Unknown")
|
||||
.WriteTo.Console()
|
||||
.WriteTo.File("logs/fallback-app-.log", rollingInterval: RollingInterval.Day);
|
||||
|
||||
// Tenta adicionar o Seq apenas se a URL estiver disponível
|
||||
if (!string.IsNullOrEmpty(seqServer))
|
||||
{
|
||||
try
|
||||
{
|
||||
config.WriteTo.Seq(seqServer);
|
||||
Console.WriteLine($"✅ Seq configurado no fallback: {seqServer}");
|
||||
}
|
||||
catch (Exception seqEx)
|
||||
{
|
||||
Console.WriteLine($"⚠️ AVISO: Seq não pôde ser configurado no fallback: {seqEx.Message}");
|
||||
Console.WriteLine("📝 Continuando apenas com console e arquivo de log");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("✅ Configuração de fallback aplicada com sucesso");
|
||||
return config;
|
||||
}
|
||||
catch (Exception fallbackEx)
|
||||
{
|
||||
Console.WriteLine($"❌ ERRO CRÍTICO: Falha na configuração de fallback: {fallbackEx.Message}");
|
||||
throw; // Re-throw porque se o fallback falhar, temos um problema sério
|
||||
}
|
||||
}
|
||||
|
||||
private static void TestSeqConnectivity(string seqUrl)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
httpClient.Timeout = TimeSpan.FromSeconds(2);
|
||||
var response = httpClient.GetAsync($"{seqUrl}/api").Result;
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
Console.WriteLine($"🌐 Conectividade com Seq OK: {seqUrl}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"⚠️ Seq configurado mas pode não estar respondendo: {response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"⚠️ Seq configurado mas conexão falhou: {ex.Message}");
|
||||
Console.WriteLine($"💡 Dica: Verifique se o Seq está rodando em {seqUrl}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//public static class SerilogConfiguration
|
||||
//{
|
||||
// public static LoggerConfiguration SetLoggerConfiguration(this WebApplicationBuilder builder, LoggerConfiguration config, IServiceProvider services, IConfiguration configuration)
|
||||
// {
|
||||
// var workspace = configuration["Serilog:Properties:Workspace"];
|
||||
// var seqServer = configuration.GetValue<string>("Serilog:WriteTo:2:Args:serverUrl"); ;
|
||||
|
||||
// config
|
||||
// .ReadFrom.Configuration(configuration)
|
||||
// .ReadFrom.Services(services)
|
||||
// .Enrich.FromLogContext()
|
||||
// .Enrich.WithEnvironmentName()
|
||||
// //.Enrich.WithMachineName()
|
||||
// .Enrich.WithProperty("Application", "SumaTube")
|
||||
// .Enrich.WithProperty("Workspace", workspace)
|
||||
// .WriteTo.Seq(seqServer)
|
||||
// ;
|
||||
|
||||
// return config;
|
||||
// }
|
||||
|
||||
//}
|
||||
}
|
||||
|
||||
@ -1,20 +1,26 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"System": "Warning"
|
||||
"WriteTo": [
|
||||
{ "Name": "Console" },
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "logs/prod-app-.log",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
},
|
||||
{
|
||||
"Name": "Seq",
|
||||
"Args": {
|
||||
"serverUrl": "http://localhost:5341",
|
||||
"compact": true,
|
||||
"batchPostingLimit": 100
|
||||
}
|
||||
}
|
||||
},
|
||||
"Enrich": [
|
||||
"FromLogContext",
|
||||
"WithMachineName",
|
||||
"WithThreadId",
|
||||
"WithEnvironmentUserName"
|
||||
],
|
||||
"Properties": {
|
||||
"Workspace": "Production",
|
||||
"Environment": "Production",
|
||||
"Workspace": "VPS",
|
||||
"Application": "YTExtractor"
|
||||
}
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user