internal Logging(string logName, string subFolder = "") { LogPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), Program.CompanyName, "Log"); if (!string.IsNullOrEmpty(subFolder) && subFolder.Length != 0) { LogPath = $"{LogPath}\\{subFolder}"; } if (!Directory.Exists(LogPath)) { Directory.CreateDirectory(LogPath); Helper.SetDirectoryNetworkServiceAccessControl(LogPath); } string fullName = Path.Combine(LogPath, $"{logName}.log"); //log4net.Config.XmlConfigurator.Configure(); Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy; hierarchy.Threshold = Level.Debug; log4net.Repository.Hierarchy.Logger logger = hierarchy.LoggerFactory.CreateLogger(hierarchy, logName); logger.Hierarchy = hierarchy; logger.AddAppender(CreateFileAppender(logName, fullName)); logger.Repository.Configured = true; logger.Level = Level.Debug; Log = new LogImpl(logger); Log.Info("**********************************************"); Log.Info($"{logName} logging started"); }
//public static void AddAppender(string appenderName, ILogger wLogger) //{ // log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)wLogger; // log4net.Appender.IAppender hasAppender = l.GetAppender(appenderName); // if (hasAppender != null) // { // hasAppender..ActivateOptions(); // l.AddAppender(hasAppender); // } //} public static void AddAppender(string appenderName, ILogger wLogger) { string filename = ConfigurationManager.AppSettings["LogDir"].ToString() + "\\" + appenderName + ".log"; log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)wLogger; log4net.Appender.IAppender hasAppender = l.GetAppender(appenderName); if (hasAppender == null) { log4net.Appender.RollingFileAppender appender = new log4net.Appender.RollingFileAppender(); appender.DatePattern = "yyyyMMdd"; appender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date; appender.AppendToFile = true; appender.File = filename; appender.StaticLogFileName = true; appender.Name = appenderName; log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout(); layout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"; layout.ActivateOptions(); appender.Layout = layout; appender.ActivateOptions(); l.AddAppender(appender); } }
public static ILog GetCustomLogger(string loggerName, string category = null, bool additivity = false) { return(loggerContainer.GetOrAdd(loggerName, delegate(string name) { RollingFileAppender newAppender = null; ReadParamAppender appender = null; if (appenderContainer.ContainsKey(loggerName)) { appender = appenderContainer[loggerName]; newAppender = GetNewFileApender(loggerName, string.IsNullOrEmpty(appender.File) ? GetFile(category, loggerName) : appender.File, appender.MaxSizeRollBackups, appender.AppendToFile, true, appender.MaximumFileSize, RollingFileAppender.RollingMode.Composite, appender.DatePattern, appender.LayoutPattern); } else { newAppender = GetNewFileApender(loggerName, GetFile(category, loggerName), MAX_SIZE_ROLL_BACKUPS, true, true, MAXIMUM_FILE_SIZE, RollingFileAppender.RollingMode.Composite, DATE_PATTERN, LAYOUT_PATTERN); } log4net.Repository.Hierarchy.Hierarchy repository = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository(); log4net.Repository.Hierarchy.Logger logger = repository.LoggerFactory.CreateLogger(repository, loggerName); logger.Hierarchy = repository; logger.Parent = repository.Root; logger.Level = GetLoggerLevel(appender == null ? LEVEL : appender.Level); logger.Additivity = additivity; logger.AddAppender(newAppender); logger.Repository.Configured = true; return new LogImpl(logger); })); }
private static void CreateLogger(FileAppender fileAppender, string loggerName) { log4net.Repository.Hierarchy.Hierarchy hierarchy = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetLoggerRepository(); log4net.Repository.Hierarchy.Logger logger = (log4net.Repository.Hierarchy.Logger)hierarchy.GetLogger(APPENDER_NAME); logger.RemoveAllAppenders(); logger.AddAppender(fileAppender); hierarchy.Configured = true; }
public LogSpy(string loggerName) { logger = (Logger)LogManager.GetLogger(loggerName).Logger; if (logger == null) throw new NullReferenceException(); prevLogLevel = logger.Level; logger.Level = Level.Debug; appender = new MemoryAppender(); logger.AddAppender(appender); }
/// <summary> /// Add an appender and see if it can be retrieved. /// </summary> [Test] public void TestAppender1() { log = LogManager.GetLogger("test").Logger as Logger; CountingAppender a1 = new CountingAppender(); a1.Name = "testAppender1"; log.AddAppender(a1); IEnumerator enumAppenders = ((IEnumerable)log.Appenders).GetEnumerator(); Assert.IsTrue( enumAppenders.MoveNext() ); CountingAppender aHat = (CountingAppender) enumAppenders.Current; Assert.AreEqual(a1, aHat); }
public TextLogSpy(string loggerName, string pattern) { stringBuilder = new StringBuilder(); appender = new TextWriterAppender { Layout = new PatternLayout(pattern), Threshold = Level.All, Writer = new StringWriter(stringBuilder) }; loggerImpl = (Logger)LogManager.GetLogger(loggerName).Logger; loggerImpl.AddAppender(appender); loggerImpl.Level = Level.All; }
public LogSpy() { logger = LogManager.GetLogger(SqlLoggerName).Logger as Logger; if (logger == null) { throw new ApplicationException("Unable to obtain logger" + SqlLoggerName); } originalLogLevel = logger.Level; logger.Level = Level.Debug; appender = new MemoryAppender(); logger.AddAppender(appender); }
public LoggerSpy(ILog log, Level level) { logger = log.Logger as Logger; if (logger == null) throw new Exception("Unable to get the logger"); prevLogLevel = logger.Level; logger.Level = level; // Add a new MemoryAppender to the logger. appender = new MemoryAppender(); logger.AddAppender(appender); }
private static void SetupLogger() { try { if (trinityLog == null) { lock (_loglock) { _Logger.Info("Setting up Trinity Logging"); int myPid = Process.GetCurrentProcess().Id; DateTime startTime = Process.GetCurrentProcess().StartTime; trinityLayout = new PatternLayout("%date{HH:mm:ss.fff} %-5level %logger{1} %m%n"); trinityLayout.ActivateOptions(); trinityFilter = new log4net.Filter.LoggerMatchFilter(); trinityFilter.LoggerToMatch = "Trinity"; trinityFilter.AcceptOnMatch = true; trinityFilter.ActivateOptions(); Hierarchy h = (Hierarchy)LogManager.GetRepository(); var appenders = h.GetAppenders(); foreach (var appender in appenders) { if (appender is FileAppender) { trinityAppender = appender as FileAppender; } } trinityAppender.Layout = trinityLayout; //trinityAppender.AddFilter(trinityFilter); trinityAppender.LockingModel = new FileAppender.ExclusiveLock(); //trinityAppender.LockingModel = new FileAppender.InterProcessLock(); trinityAppender.ActivateOptions(); trinityLog = LogManager.GetLogger("TrinityDebug"); trinityLogger = ((log4net.Repository.Hierarchy.Logger)trinityLog.Logger); trinityLogger.Additivity = false; trinityLogger.AddAppender(trinityAppender); } } } catch (Exception ex) { _Logger.Error("Error setting up Trinity Logger:\n" + ex.ToString()); } }
public SqlLogSpy() { ILog log = LogManager.GetLogger("NHibernate.SQL"); sqlLogger = log.Logger as Logger; if (sqlLogger == null) throw new Exception("Unable to get the SQL logger"); // Change the log level to DEBUG and temporarily save the previous log level prevLogLevel = sqlLogger.Level; sqlLogger.Level = Level.Debug; // Add a new MemoryAppender to the logger. appender = new MemoryAppender(); sqlLogger.AddAppender(appender); }
public LogSpy(ILog log, Level level) { logger = log.Logger as Logger; if (logger == null) { throw new Exception("Unable to get the logger"); } // Change the log level to DEBUG and temporarily save the previous log level prevLogLevel = logger.Level; logger.Level = level; // Add a new MemoryAppender to the logger. appender = new MemoryAppender(); logger.AddAppender(appender); }
// Add an appender to a logger public static void AddAppender(string loggerName, log4net.Appender.IAppender appender) { try { log4net.ILog log = log4net.LogManager.GetLogger(loggerName); log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)log.Logger; l.AddAppender(appender); } catch (Exception er) { using (StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\FatalLogError.log", true)) { sw.WriteLine(er.Message); } } }
/// <summary> /// Parses the children of a logger element. /// </summary> /// <param name="catElement">The category element.</param> /// <param name="log">The logger instance.</param> /// <param name="isRoot">Flag to indicate if the logger is the root logger.</param> /// <remarks> /// <para> /// Parse the child elements of a <logger> element. /// </para> /// </remarks> protected void ParseChildrenOfLoggerElement(XmlElement catElement, Logger log, bool isRoot) { // Remove all existing appenders from log. They will be // reconstructed if need be. log.RemoveAllAppenders(); foreach (XmlNode currentNode in catElement.ChildNodes) { if (currentNode.NodeType == XmlNodeType.Element) { XmlElement currentElement = (XmlElement)currentNode; if (currentElement.LocalName == APPENDER_REF_TAG) { IAppender appender = FindAppenderByReference(currentElement); string refName = currentElement.GetAttribute(REF_ATTR); if (appender != null) { LogLog.Debug(declaringType, "Adding appender named [" + refName + "] to logger [" + log.Name + "]."); log.AddAppender(appender); } else { LogLog.Error(declaringType, "Appender named [" + refName + "] not found."); } } else if (currentElement.LocalName == LEVEL_TAG || currentElement.LocalName == PRIORITY_TAG) { ParseLevel(currentElement, log, isRoot); } else { SetParameter(currentElement, log); } } } IOptionHandler optionHandler = log as IOptionHandler; if (optionHandler != null) { optionHandler.ActivateOptions(); } }
/// <summary> /// Log4net사용을 위한 설정 /// </summary> /// <param name="logLevel">로그 레벨 설정</param> public void SetupLog4net(Level logLevel, string loggerName) { CheckAndCreateLoggingFolder(); ILoggerRepository repository = LogManager.CreateRepository(loggerName); Hierarchy hierarchy = (Hierarchy)repository; log4net.Repository.Hierarchy.Logger logger = hierarchy.LoggerFactory.CreateLogger((ILoggerRepository)hierarchy, loggerName); logger.Hierarchy = hierarchy; CreateFileAppender(loggerName); logger.AddAppender(this.roller); logger.Repository.Configured = true; hierarchy.Threshold = logLevel; logger.Level = logLevel; this.log = new LogImpl(logger); }
/// <summary> /// Log4net사용을 위한 설정 /// </summary> /// <param name="logLevel">로그 레벨 설정</param> public void SetupLog4net(Level logLevel, string loggerName) { CheckAndCreateLoggingFolder(); //로그파일이 없을때만 생성하도록 변경해야 함 (ihlee) bool exists = false; ILoggerRepository repository = null; log4net.Repository.ILoggerRepository[] repositories = LogManager.GetAllRepositories(); if (repositories != null) { foreach (log4net.Repository.ILoggerRepository r in repositories) { if (r.Name == loggerName) { repository = r; exists = true; break; } } } if (!exists) { repository = LogManager.CreateRepository(loggerName); } Hierarchy hierarchy = (Hierarchy)repository; log4net.Repository.Hierarchy.Logger logger = hierarchy.LoggerFactory.CreateLogger((ILoggerRepository)hierarchy, loggerName); logger.Hierarchy = hierarchy; CreateFileAppender(loggerName); logger.AddAppender(this.roller); logger.Repository.Configured = true; hierarchy.Threshold = logLevel; logger.Level = logLevel; this.log = new LogImpl(logger); }
/// <summary> /// Test additivity flag. /// </summary> [Test] public void TestAdditivity3() { Logger root = ((Hierarchy)LogManager.GetLoggerRepository()).Root; Logger a = LogManager.GetLogger("a").Logger as Logger; Logger ab = LogManager.GetLogger("a.b").Logger as Logger; Logger abc = LogManager.GetLogger("a.b.c").Logger as Logger; Logger x = LogManager.GetLogger("x").Logger as Logger; CountingAppender caRoot = new CountingAppender(); CountingAppender caA = new CountingAppender(); CountingAppender caABC = new CountingAppender(); root.AddAppender(caRoot); a.AddAppender(caA); abc.AddAppender(caABC); a.Repository.Configured = true; Assertion.AssertEquals(caRoot.Counter, 0); Assertion.AssertEquals(caA.Counter, 0); Assertion.AssertEquals(caABC.Counter, 0); ab.Additivity = false; a.Log(Level.DEBUG, MSG, null); Assertion.AssertEquals(caRoot.Counter, 1); Assertion.AssertEquals(caA.Counter, 1); Assertion.AssertEquals(caABC.Counter, 0); ab.Log(Level.DEBUG, MSG, null); Assertion.AssertEquals(caRoot.Counter, 1); Assertion.AssertEquals(caA.Counter, 1); Assertion.AssertEquals(caABC.Counter, 0); abc.Log(Level.DEBUG, MSG, null); Assertion.AssertEquals(caRoot.Counter, 1); Assertion.AssertEquals(caA.Counter, 1); Assertion.AssertEquals(caABC.Counter, 1); }
private void Configure() { _log = ((log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository()).Root; _log.AddAppender(CreateFileAppender("FileAppender", System.IO.Path.Combine(Environment.CurrentDirectory, "wpftdm.log"))); log4net.GlobalContext.Properties["LogFolderLocation"] = Environment.CurrentDirectory; BasicConfigurator.Configure(); EventAggregator = new EventAggregator(); }
/// <summary> /// Add an appender and see if it can be retrieved. /// </summary> [Test] public void TestAppender1() { log = LogManager.GetLogger("test").Logger as Logger; CountingAppender a1 = new CountingAppender(); a1.Name = "testAppender1"; log.AddAppender(a1); IEnumerator enumAppenders = log.Appenders.GetEnumerator(); Assertion.Assert( enumAppenders.MoveNext() ); CountingAppender aHat = (CountingAppender) enumAppenders.Current; Assertion.AssertEquals(a1, aHat); }
/// <summary> /// Sets the Appender that the ADK will use for logging. /// </summary> /// <param name="logger"></param> /// <param name="appender"></param> /// <param name="level"></param> private static void SetLogAppender(Logger logger, IAppender appender, Level level) { if (logger != null) { logger.RemoveAllAppenders(); logger.AddAppender(appender); logger.Repository.Threshold = level; logger.Repository.Configured = true; } else { throw new AdkException ("Unable to initialize log4net framework, ADK Logger is null", null); } }
/// <summary> /// Configures the root appender for counting and rolling /// </summary> private void ConfigureRootAppender() { _root = ((Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root; _root.Level = Level.Debug; _caRoot = new CountingAppender(); _root.AddAppender(_caRoot); Assert.AreEqual(_caRoot.Counter, 0); // // Set the root appender with a RollingFileAppender // _root.AddAppender(CreateAppender()); _root.Repository.Configured = true; }
public SyncServiceLogAppender(string fileLocation, long storeLocationID) { log4net.Repository.Hierarchy.Hierarchy repository = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository(); if (fileLocation != "") { LoggerSimple.WriteMessage("Creating logger...." + fileLocation); var appenders = repository.GetAppenders(); if (appenders.FirstOrDefault(i => i.Name == "ASyncServiceLogAppender-" + storeLocationID) != null) { return; } SyncServiceFileAppender originalAppender = appenders.FirstOrDefault(i => i.Name == "SyncServiceLogAppender-0") as SyncServiceFileAppender; //(appenders[0] as SyncServiceFileAppender); SyncServiceFileAppender fileAppender = new SyncServiceFileAppender(); fileAppender.Name = "ASyncServiceLogAppender-" + storeLocationID; fileAppender.File = fileLocation; fileAppender.AppendToFile = true; fileAppender.MaxSizeRollBackups = originalAppender.MaxSizeRollBackups; fileAppender.MaximumFileSize = originalAppender.MaximumFileSize; fileAppender.RollingStyle = originalAppender.RollingStyle; fileAppender.MaximumFileSize = originalAppender.MaximumFileSize; fileAppender.CountDirection = originalAppender.CountDirection; fileAppender.DatePattern = originalAppender.DatePattern; fileAppender.ImmediateFlush = originalAppender.ImmediateFlush; fileAppender.MaxFileSize = originalAppender.MaxFileSize; fileAppender.StaticLogFileName = false; fileAppender.Threshold = log4net.Core.Level.All; fileAppender.Layout = originalAppender.Layout; fileAppender.LockingModel = new FileAppender.MinimalLock(); fileAppender.ActivateOptions(); fileAppender.FilePath = fileLocation; log4net.Repository.Hierarchy.Logger logger = (log4net.Repository.Hierarchy.Logger)repository.GetLogger("ASyncServiceLogAppender-" + storeLocationID); logger.AddAppender(fileAppender); LoggerSimple.WriteMessage("Logger Created..." + storeLocationID); Writer = LogManager.GetLogger("ASyncServiceLogAppender-" + storeLocationID); } else { Writer = LogManager.GetLogger("SyncServiceLogAppender-" + storeLocationID); } }
public RecieverFailure() { logger = ((Logger)(LogManager.GetLogger(typeof(Receiver)).Logger)); logger.AddAppender(appender); }
private void AddAppenderToLogger(Logger logger, QueryAppender appender) { lock (logger) { logger.Additivity = true; if (HasQueryAppender(logger) == false) { logger.AddAppender(appender); logger.Level = logger.Hierarchy.LevelMap["DEBUG"]; } } }
/// <summary> /// Parses the children of a logger element. /// </summary> /// <param name="catElement">The category element.</param> /// <param name="log">The logger instance.</param> /// <param name="isRoot">Flag to indicate if the logger is the root logger.</param> /// <remarks> /// <para> /// Parse the child elements of a <logger> element. /// </para> /// </remarks> protected void ParseChildrenOfLoggerElement(XmlElement catElement, Logger log, bool isRoot) { // Remove all existing appenders from log. They will be // reconstructed if need be. log.RemoveAllAppenders(); foreach (XmlNode currentNode in catElement.ChildNodes) { if (currentNode.NodeType == XmlNodeType.Element) { XmlElement currentElement = (XmlElement) currentNode; if (currentElement.LocalName == APPENDER_REF_TAG) { IAppender appender = FindAppenderByReference(currentElement); string refName = currentElement.GetAttribute(REF_ATTR); if (appender != null) { LogLog.Debug(declaringType, "Adding appender named [" + refName + "] to logger [" + log.Name + "]."); log.AddAppender(appender); } else { LogLog.Error(declaringType, "Appender named [" + refName + "] not found."); } } else if (currentElement.LocalName == LEVEL_TAG || currentElement.LocalName == PRIORITY_TAG) { ParseLevel(currentElement, log, isRoot); } else { SetParameter(currentElement, log); } } } IOptionHandler optionHandler = log as IOptionHandler; if (optionHandler != null) { optionHandler.ActivateOptions(); } }
private void AddAppender(ILog log, IAppender appender) { log4net.Repository.Hierarchy.Logger logger = (log4net.Repository.Hierarchy.Logger)log.Logger; logger.AddAppender(appender); }
/// <summary> /// Add an appender X, Y, remove X and check if Y is the only /// remaining appender. /// </summary> [Test] public void TestAppender2() { CountingAppender a1 = new CountingAppender(); a1.Name = "testAppender2.1"; CountingAppender a2 = new CountingAppender(); a2.Name = "testAppender2.2"; log = LogManager.GetLogger("test").Logger as Logger; log.AddAppender(a1); log.AddAppender(a2); CountingAppender aHat = (CountingAppender)log.GetAppender(a1.Name); Assert.AreEqual(a1, aHat); aHat = (CountingAppender)log.GetAppender(a2.Name); Assert.AreEqual(a2, aHat); log.RemoveAppender("testAppender2.1"); IEnumerator enumAppenders = ((IEnumerable)log.Appenders).GetEnumerator(); Assert.IsTrue( enumAppenders.MoveNext() ); aHat = (CountingAppender) enumAppenders.Current; Assert.AreEqual(a2, aHat); Assert.IsTrue(!enumAppenders.MoveNext()); aHat = (CountingAppender)log.GetAppender(a2.Name); Assert.AreEqual(a2, aHat); }
// Add an appender to a logger public static void AddAppender(string loggerName, log4net.Appender.IAppender appender) { log4net.ILog log = log4net.LogManager.GetLogger(loggerName); log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)log.Logger; l.AddAppender(appender); }