/// <summary> /// Sets logging level for all current loggers to the level provided in arguments. /// Note: use it only when you need more control on logging, e.g. in unit tests. Otherwise use configuration files. /// </summary> /// <param name="level"></param> public static void SetLoggingLevel(Level level) { var repositories = LogManager.GetAllRepositories(); //Configure all loggers to be at the debug level. foreach (var repository in repositories) { repository.Threshold = repository.LevelMap[level.ToString()]; var hierarchy = (log4net.Repository.Hierarchy.Hierarchy)repository; var loggers = hierarchy.GetCurrentLoggers(); foreach (var logger in loggers) { ((log4net.Repository.Hierarchy.Logger)logger).Level = hierarchy.LevelMap[level.ToString()]; } } //Configure the root logger. var h = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository(); var rootLogger = h.Root; rootLogger.Level = h.LevelMap[level.ToString()]; }
public static void RemoveMatchingLogEntries(Level level, string message, string application) { string commandText = String.Format("DELETE FROM dbo.Log4Net WHERE Level = @Level AND Message = @Message AND Application = @Application"); using (SqlConnection sqlConnection = new SqlConnection(ConnectionString)) { sqlConnection.Open(); using (SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection)) { sqlCommand.Parameters.Add(new SqlParameter("Level", level.ToString())); sqlCommand.Parameters.Add(new SqlParameter("Message", message)); sqlCommand.Parameters.Add(new SqlParameter("Application", application)); sqlCommand.ExecuteNonQuery(); } } }
public static int CountLogEntriesPresent(Level level, string message, string application) { string commandText = String.Format("SELECT COUNT(*) FROM dbo.Log4Net (nolock) WHERE Level = @Level AND Message = @Message AND Application = @Application"); using (SqlConnection sqlConnection = new SqlConnection(ConnectionString)) { sqlConnection.Open(); using (SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection)) { sqlCommand.Parameters.Add(new SqlParameter("Level", level.ToString())); sqlCommand.Parameters.Add(new SqlParameter("Message", message)); sqlCommand.Parameters.Add(new SqlParameter("Application", application)); int rowCount = (int)sqlCommand.ExecuteScalar(); return rowCount; } } }
public static bool IsLogEntryPresent(Level level, string message, string application) { string commandText = String.Format("SELECT * FROM dbo.Log4Net WHERE Level = @Level AND Message = @Message AND Application = @Application"); using (SqlConnection sqlConnection = new SqlConnection(ConnectionString)) { sqlConnection.Open(); using (SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection)) { sqlCommand.Parameters.Add(new SqlParameter("Level", level.ToString())); sqlCommand.Parameters.Add(new SqlParameter("Message", message)); sqlCommand.Parameters.Add(new SqlParameter("Application", application)); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); int fieldCount = 0; while (sqlDataReader.Read()) { fieldCount++; } return fieldCount == 1; } } }
private static SelectListItem CreateSelectItem(Level level) { SelectListItem sel = new SelectListItem(); sel.Text = level.DisplayName; sel.Value = level.ToString(); return sel; }
private static string GetLevelName(Level code) { return string.Format("Log{0}", code.ToString()).ToLower(); }
void ILogger.Log(Type callerStackBoundaryDeclaringType, Level level, object message, Exception exception) { tbLog.Text += string.Format("{0} - {1}, {2}", level.ToString(),message.ToString(), exception.ToString()); }
internal static void LogToFile(string logFile, string message, Level level, string category, Exception exception) { string formatedMessage = null; if (exception == null) { formatedMessage = String.Format("{0} {1,-5} {2,-8} - {3}", new object[]{ DateTime.Now.ToString("MM-dd HH:mm:ss.fff"), level.ToString(), category, message }); } else if (!String.IsNullOrEmpty(message)) { formatedMessage = String.Format("{0} {1,-5} {2,-8} - {3}\r\n{4}", new object[]{ DateTime.Now.ToString("MM-dd HH:mm:ss.fff"), level.ToString(), category, message, exception.ToString() }); } else { formatedMessage = String.Format("{0} {1,-5} {2,-8} - {3}", new object[]{ DateTime.Now.ToString("MM-dd HH:mm:ss.fff"), level.ToString(), category, exception.ToString() }); } try { int retryCount = 0; while (true) { retryCount++; try { string fileName = AppDomain.CurrentDomain.MapPhysicalPath("logs\\" + logFile); if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(fileName))) { System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(fileName)); } using (System.IO.FileStream fs = new System.IO.FileStream( GetCurrentLogFile(fileName), System.IO.FileMode.Append, System.IO.FileAccess.Write, FileShare.Read) ) { using (System.IO.StreamWriter w = new System.IO.StreamWriter(fs, Encoding.Default)) { w.WriteLine(formatedMessage); } } break; } catch { if (retryCount >= 5) { throw; } System.Threading.Thread.Sleep(200); } } } catch (Exception err) { LogToUnhandledExceptions( String.Format("{0}\r\n原始要写入的消息为:\r\n{1}", err.GetFriendlyToString(), message) ); } }
private static SelectListItem CreateSelectItem(Level level) { var sel = new SelectListItem { Text = level.DisplayName, Value = level.ToString() }; return sel; }