private void SendConsole(BotLogMessage log) { if (!_options.UseColorOutput) { Console.WriteLine($"{log.GetTimestamp()} [{log.GetShortLogLevel()}] {log.SourceName}: {log.Content}"); return; } Console.Write(log.GetTimestamp(), Color.Gray); Color levelColor; switch (log.LogLevel) { case LogLevel.Trace: levelColor = Color.White; break; case LogLevel.Information: levelColor = Color.Green; break; case LogLevel.Warning: levelColor = Color.Yellow; break; case LogLevel.Debug: levelColor = Color.Purple; break; case LogLevel.Error: levelColor = Color.Red; break; case LogLevel.Critical: levelColor = Color.Red; break; default: levelColor = Color.White; break; } Colorful.Console.Write($" [{log.GetShortLogLevel()}] ", levelColor); Colorful.Console.Write(log.SourceName, Color.DarkGray); Colorful.Console.Write(": ", Color.DarkGray); Colorful.Console.Write(log.Content, Color.White); Console.WriteLine(); }
public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { var log = new BotLogMessage { LogLevel = logLevel, SourceName = _categoryName, Content = formatter(state, exception) }; string logText = log.ToString(); if (!Directory.Exists(_outputDirectory)) { Directory.CreateDirectory(_outputDirectory); } var fileInfo = new FileInfo(_logFile); if (!fileInfo.Exists) { fileInfo.Create().Dispose(); fileInfo.Refresh(); } if (fileInfo.Length + logText.Length > _options.MaxFileSizeKb * 1000) { _duplicateLogFileCount++; } try { using (var writer = fileInfo.AppendText()) writer.Write(logText); } catch { } SendConsole(log); Debug.Write(logText); }