private void LogFile <TState>(string categoryName, LogLevel logLevel, string message, Exception exception) { string type; switch (logLevel) { case LogLevel.Information: type = " INFORMATION "; break; case LogLevel.Error: type = " ERROR "; break; case LogLevel.Critical: type = " CRITICAL "; break; case LogLevel.Debug: type = " DEBUG "; break; case LogLevel.Trace: type = " TRACE "; break; case LogLevel.Warning: type = " WARNING "; break; default: type = " INFORMATION "; break; } DateTime timestamp = DateTime.Now; try { StackTrace stackTrace = new StackTrace(); _provider.AddMessage(timestamp, new LogMessage() { Timestamp = timestamp, DebugLevel = type, CategoryName = categoryName, Exception = exception, Message = message, MethodBase = stackTrace.GetFrame(stackTrace.FrameCount - 1).GetMethod(), Type = typeof(T), TypeTState = typeof(TState) }); } catch { _provider.AddMessage(timestamp, new LogMessage() { Timestamp = timestamp, DebugLevel = type, CategoryName = categoryName, Exception = exception, Message = message, MethodBase = null, Type = typeof(T), TypeTState = typeof(TState) }); } }
public void Log <TState>(DateTimeOffset timestamp, LogLevel logLevel, EventId _, TState state, Exception exception, Func <TState, Exception, string> formatter) { if (!IsEnabled(logLevel)) { return; } var builder = new StringBuilder(); builder.Append(timestamp.ToString("yyyy-MM-dd HH:mm:ss.fff zzz", CultureInfo.InvariantCulture)); builder.Append(" ["); builder.Append(logLevel.ToString()); builder.Append("] "); builder.Append(_category); var scopeProvider = _provider.ScopeProvider; if (scopeProvider != null) { scopeProvider.ForEachScope((scope, stringBuilder) => { stringBuilder.Append(" => ").Append(scope); }, builder); builder.AppendLine(":"); } else { builder.Append(": "); } builder.AppendLine(formatter(state, exception)); if (exception != null) { builder.AppendLine(exception.ToString()); } _provider.AddMessage(timestamp, builder.ToString()); }