public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { var log = new DogeyLogMessage { 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(); } if (fileInfo.Length + logText.Length > _maxFileSizeKb * 1000) { _duplicateLogFileCount++; } File.AppendAllText(_logFile, logText); SendConsole(log); }
private void SendConsole(DogeyLogMessage log) { Console.Write(log.GetTimestamp(), Color.Gray); Color levelColor; switch (log.LogLevel) { case LogLevel.Trace: levelColor = Color.WhiteSmoke; break; case LogLevel.Information: levelColor = Color.LightGreen; break; case LogLevel.Warning: levelColor = Color.Yellow; break; case LogLevel.Debug: levelColor = Color.LightBlue; break; case LogLevel.Error: levelColor = Color.Red; break; case LogLevel.Critical: levelColor = Color.DarkRed; break; default: levelColor = Color.White; break; } Console.Write(" [", levelColor); Console.Write(log.GetShortLogLevel(), levelColor); Console.Write("] ", levelColor); Console.Write(log.SourceName, Color.DarkGray); Console.Write(": ", Color.DarkGray); Console.Write(log.Content, Color.White); Console.WriteLine(); }