/// <summary> /// The log. /// </summary> /// <param name="message"> /// The message. /// </param> /// <param name="eventType"> /// The event type. /// </param> /// <param name="userId"> /// The user Id. /// </param> /// <param name="source"> /// The source. /// </param> /// <param name="exception"> /// The exception. /// </param> public void Log( string message, EventLogTypes eventType = EventLogTypes.Error, int?userId = null, string source = null, Exception exception = null) { if (!this.IsLogTypeEnabled(eventType)) { return; } var exceptionDescription = string.Empty; if (exception != null) { exceptionDescription = exception.ToString(); } var formattedDescription = HttpContext.Current != null ? $"{message} (URL:'{HttpContext.Current.Request.Url}')\r\n{exceptionDescription}" : $"{message}\r\n{exceptionDescription}"; if (source.IsNotSet()) { if (this.Type != null) { source = this.Type.FullName.Length > 50 ? this.Type.FullName.Truncate(47) : this.Type.FullName; } else { source = string.Empty; } } switch (eventType) { case EventLogTypes.Debug: Debug.WriteLine(formattedDescription, source); break; case EventLogTypes.Trace: Trace.TraceInformation(formattedDescription); if (exception != null) { Trace.TraceError(exception.ToString()); } break; case EventLogTypes.Information: if (BoardContext.Current.Get <BoardSettings>().LogInformation) { this.EventLogRepository.Insert( new EventLog { EventType = eventType, UserID = userId, Description = formattedDescription, Source = source, EventTime = DateTime.UtcNow }); } break; case EventLogTypes.Warning: if (BoardContext.Current.Get <BoardSettings>().LogWarning) { this.EventLogRepository.Insert( new EventLog { EventType = eventType, UserID = userId, Description = formattedDescription, Source = source, EventTime = DateTime.UtcNow }); } break; case EventLogTypes.Error: if (BoardContext.Current.Get <BoardSettings>().LogError) { this.EventLogRepository.Insert( new EventLog { EventType = eventType, UserID = userId, Description = formattedDescription, Source = source, EventTime = DateTime.UtcNow }); } break; default: { var log = new EventLog { EventType = eventType, UserID = userId, Description = formattedDescription, Source = source, EventTime = DateTime.UtcNow }; this.EventLogRepository.Insert(log); } break; } }
/// <summary> /// The log. /// </summary> /// <param name="message"> /// The message. /// </param> /// <param name="eventType"> /// The event type. /// </param> /// <param name="username"> /// The username. /// </param> /// <param name="source"> /// The source. /// </param> /// <param name="exception"> /// The exception. /// </param> public void Log( string message, EventLogTypes eventType = EventLogTypes.Error, string username = null, string source = null, Exception exception = null) { if (!this.IsLogTypeEnabled(eventType)) { return; } var exceptionDescription = string.Empty; if (exception != null) { exceptionDescription = exception.ToString(); } var formattedDescription = message + "\r\n" + exceptionDescription; switch (eventType) { case EventLogTypes.Debug: Debug.WriteLine(formattedDescription, source); break; case EventLogTypes.Trace: Trace.TraceInformation(formattedDescription); if (exception != null) { Trace.TraceError(exception.ToString()); } break; default: { if (source.IsNotSet()) { if (this.Type != null) { source = this.Type.FullName.Length > 50 ? this.Type.FullName.Truncate(47) : this.Type.FullName; } else { source = string.Empty; } } var log = new EventLog { EventType = eventType, UserName = username, Description = formattedDescription, Source = source, EventTime = DateTime.UtcNow }; this.EventLogRepository.Insert(log); } break; } }
/// <summary> /// The log. /// </summary> /// <param name="message"> /// The message. /// </param> /// <param name="eventType"> /// The event type. /// </param> /// <param name="username"> /// The username. /// </param> /// <param name="source"> /// The source. /// </param> /// <param name="exception"> /// The exception. /// </param> public void Log( string message, EventLogTypes eventType = EventLogTypes.Error, string username = null, string source = null, Exception exception = null) { if (!this.IsLogTypeEnabled(eventType)) { return; } var exceptionDescription = string.Empty; if (exception != null) { exceptionDescription = exception.ToString(); } var formattedDescription = HttpContext.Current != null ? "{0} (URL:'{1}')\r\n{2}".FormatWith( message, HttpContext.Current.Request.Url, exceptionDescription) : "{0}\r\n{1}".FormatWith(message, exceptionDescription); if (source.IsNotSet()) { if (this.Type != null) { source = this.Type.FullName.Length > 50 ? this.Type.FullName.Truncate(47) : this.Type.FullName; } else { source = string.Empty; } } switch (eventType) { case EventLogTypes.Debug: Debug.WriteLine(formattedDescription, source); break; case EventLogTypes.Trace: Trace.TraceInformation(formattedDescription); if (exception != null) { Trace.TraceError(exception.ToString()); } break; case EventLogTypes.Information: if (YafContext.Current.Get<YafBoardSettings>().LogInformation) { this.EventLogRepository.Insert( new EventLog { EventType = eventType, UserName = username, Description = formattedDescription, Source = source, EventTime = DateTime.UtcNow }); } break; case EventLogTypes.Warning: if (YafContext.Current.Get<YafBoardSettings>().LogWarning) { this.EventLogRepository.Insert( new EventLog { EventType = eventType, UserName = username, Description = formattedDescription, Source = source, EventTime = DateTime.UtcNow }); } break; case EventLogTypes.Error: if (YafContext.Current.Get<YafBoardSettings>().LogError) { this.EventLogRepository.Insert( new EventLog { EventType = eventType, UserName = username, Description = formattedDescription, Source = source, EventTime = DateTime.UtcNow }); } break; default: { var log = new EventLog { EventType = eventType, UserName = username, Description = formattedDescription, Source = source, EventTime = DateTime.UtcNow }; this.EventLogRepository.Insert(log); } break; } }