Esempio n. 1
0
        private static void ConfigureGlobalLogger()
        {
            Serilog.Log.Logger = (OptionsAtStartup.Debug ? new LoggerConfiguration().MinimumLevel.Debug() : new LoggerConfiguration().MinimumLevel.Information())
                                 .MinimumLevel.Override("Microsoft", LogEventLevel.Error)
                                 .MinimumLevel.Override("System.Net.Http.HttpClient", OptionsAtStartup.Debug ? LogEventLevel.Warning : LogEventLevel.Fatal)
                                 .MinimumLevel.Override("slskd.Authentication.PassthroughAuthenticationHandler", LogEventLevel.Warning)
                                 .Enrich.WithProperty("InstanceName", OptionsAtStartup.InstanceName)
                                 .Enrich.WithProperty("InvocationId", InvocationId)
                                 .Enrich.WithProperty("ProcessId", ProcessId)
                                 .Enrich.FromLogContext()
                                 .WriteTo.Console(
                outputTemplate: (OptionsAtStartup.Debug ? "[{SubContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
                                 .WriteTo.Async(config =>
                                                config.File(
                                                    Path.Combine(AppDirectory, "logs", $"{AppName}-.log"),
                                                    outputTemplate: (OptionsAtStartup.Debug ? "[{SubContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
                                                    rollingInterval: RollingInterval.Day))
                                 .WriteTo.Conditional(
                e => !string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki),
                config => config.GrafanaLoki(
                    OptionsAtStartup.Logger.Loki ?? string.Empty,
                    outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"))
                                 .WriteTo.Sink(new DelegatingSink(logEvent =>
            {
                try
                {
                    var message = logEvent.RenderMessage();

                    if (logEvent.Exception != null)
                    {
                        message = $"{message}: {logEvent.Exception}";
                    }

                    var record = new LogRecord()
                    {
                        Timestamp  = logEvent.Timestamp.LocalDateTime,
                        Context    = logEvent.Properties["SourceContext"].ToString().TrimStart('"').TrimEnd('"'),
                        SubContext = logEvent.Properties.ContainsKey("SubContext") ? logEvent.Properties["SubContext"].ToString().TrimStart('"').TrimEnd('"') : null,
                        Level      = logEvent.Level.ToString(),
                        Message    = message.TrimStart('"').TrimEnd('"'),
                    };

                    LogBuffer.Enqueue(record);
                    LogEmitted?.Invoke(null, record);
                }
                catch (Exception ex)
                {
                    Log.Information($"Misconfigured delegating logger: {ex.Message}");
                }
            }))
                                 .CreateLogger();
        }
Esempio n. 2
0
        public void Emit(LogEvent logEvent)
        {
            if (logEvent.Level >= AppSettingsManager.GetConsoleMinLogLevel())
            {
                var        t   = logEvent.Timestamp;
                LogMessage msg = new LogMessage()
                {
                    Text      = logEvent.RenderMessage(),
                    Lvl       = logEvent.Level,
                    TimeStamp = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff") + " -" + t.Offset.ToString(@"hh\:mm"),
                    Exception = logEvent.Exception
                };

                LogEmitted?.Invoke(msg);
            }
        }
Esempio n. 3
0
 public static void Exception(Exception ex)
 => LogEmitted?.Invoke(null, new GenericEventArgs <string>(ConstructMessage(ex)));
Esempio n. 4
0
 public static void Error(string message)
 => LogEmitted?.Invoke(null, new GenericEventArgs <string>(ConstructMessage(message, "ERR")));
Esempio n. 5
0
 public static void Warning(string message)
 => LogEmitted?.Invoke(null, new GenericEventArgs <string>(ConstructMessage(message, "WRN")));