Compare commits
No commits in common. "092ea916b92ad07c97557659c8808987a143f591" and "c5f41ab3967cc335b0fb546ebfcb747c72de65b4" have entirely different histories.
092ea916b9
...
c5f41ab396
@ -7,139 +7,24 @@ namespace YTExtractor.Logging.Configuration
|
|||||||
{
|
{
|
||||||
public static class SerilogConfiguration
|
public static class SerilogConfiguration
|
||||||
{
|
{
|
||||||
public static LoggerConfiguration SetLoggerConfiguration(
|
public static LoggerConfiguration SetLoggerConfiguration(this WebApplicationBuilder builder, LoggerConfiguration config, IServiceProvider services, IConfiguration configuration)
|
||||||
this WebApplicationBuilder builder,
|
|
||||||
LoggerConfiguration config,
|
|
||||||
IServiceProvider services,
|
|
||||||
IConfiguration configuration)
|
|
||||||
{
|
{
|
||||||
var workspace = configuration["Serilog:Properties:Workspace"];
|
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"); ;
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Usa a configuração do appsettings.json (mais elegante)
|
|
||||||
config
|
config
|
||||||
.ReadFrom.Configuration(configuration)
|
.ReadFrom.Configuration(configuration)
|
||||||
.ReadFrom.Services(services)
|
.ReadFrom.Services(services)
|
||||||
.Enrich.FromLogContext()
|
.Enrich.FromLogContext()
|
||||||
.Enrich.WithEnvironmentName()
|
.Enrich.WithEnvironmentName()
|
||||||
//.Enrich.WithMachineName()
|
//.Enrich.WithMachineName()
|
||||||
.Enrich.WithProperty("Application", "YTExtractor")
|
.Enrich.WithProperty("Application", "SumaTube")
|
||||||
.Enrich.WithProperty("Workspace", workspace);
|
.Enrich.WithProperty("Workspace", workspace)
|
||||||
|
.WriteTo.Seq(seqServer)
|
||||||
// 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;
|
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,26 +1,20 @@
|
|||||||
{
|
{
|
||||||
"Serilog": {
|
"Serilog": {
|
||||||
"WriteTo": [
|
"MinimumLevel": {
|
||||||
{ "Name": "Console" },
|
"Default": "Information",
|
||||||
{
|
"Override": {
|
||||||
"Name": "File",
|
"Microsoft": "Warning",
|
||||||
"Args": {
|
"System": "Warning"
|
||||||
"path": "logs/prod-app-.log",
|
|
||||||
"rollingInterval": "Day"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
"Enrich": [
|
||||||
"Name": "Seq",
|
"FromLogContext",
|
||||||
"Args": {
|
"WithMachineName",
|
||||||
"serverUrl": "http://localhost:5341",
|
"WithThreadId",
|
||||||
"compact": true,
|
"WithEnvironmentUserName"
|
||||||
"batchPostingLimit": 100
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
],
|
||||||
"Properties": {
|
"Properties": {
|
||||||
"Environment": "Production",
|
"Workspace": "Production",
|
||||||
"Workspace": "VPS",
|
|
||||||
"Application": "YTExtractor"
|
"Application": "YTExtractor"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user