Merge pull request 'fix: seq disponivel eventualmente.' (#12) from release/V1.0.0 into main
All checks were successful
Build and Deploy ASP.NET API / build-and-deploy (push) Successful in 1m25s
All checks were successful
Build and Deploy ASP.NET API / build-and-deploy (push) Successful in 1m25s
Reviewed-on: http://git.carneiro.ddnsfree.com/ricardo/YTExtractor/pulls/12
This commit is contained in:
commit
092ea916b9
@ -7,24 +7,139 @@ namespace YTExtractor.Logging.Configuration
|
|||||||
{
|
{
|
||||||
public static class SerilogConfiguration
|
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 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", "SumaTube")
|
.Enrich.WithProperty("Application", "YTExtractor")
|
||||||
.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,20 +1,26 @@
|
|||||||
{
|
{
|
||||||
"Serilog": {
|
"Serilog": {
|
||||||
"MinimumLevel": {
|
"WriteTo": [
|
||||||
"Default": "Information",
|
{ "Name": "Console" },
|
||||||
"Override": {
|
{
|
||||||
"Microsoft": "Warning",
|
"Name": "File",
|
||||||
"System": "Warning"
|
"Args": {
|
||||||
|
"path": "logs/prod-app-.log",
|
||||||
|
"rollingInterval": "Day"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Enrich": [
|
{
|
||||||
"FromLogContext",
|
"Name": "Seq",
|
||||||
"WithMachineName",
|
"Args": {
|
||||||
"WithThreadId",
|
"serverUrl": "http://localhost:5341",
|
||||||
"WithEnvironmentUserName"
|
"compact": true,
|
||||||
|
"batchPostingLimit": 100
|
||||||
|
}
|
||||||
|
}
|
||||||
],
|
],
|
||||||
"Properties": {
|
"Properties": {
|
||||||
"Workspace": "Production",
|
"Environment": "Production",
|
||||||
|
"Workspace": "VPS",
|
||||||
"Application": "YTExtractor"
|
"Application": "YTExtractor"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user