Log4Net: Programmatically specify multiple loggers (with multiple file appenders)

This thread at the log4net Dashboard details an approach. To summarize a little, hopefully without ripping off too much code: using log4net; using log4net.Appender; using log4net.Layout; using log4net.Repository.Hierarchy; // Set the level for a named logger public static void SetLevel(string loggerName, string levelName) { ILog log = LogManager.GetLogger(loggerName); Logger l = (Logger)log.Logger; l.Level = l.Hierarchy.LevelMap[levelName]; … Read more

Why isn’t there a trace level in log4Net?

You can add a Verbose (or Trace level) to log4net by using extension methods. This is what I’m using: public static class ILogExtentions { public static void Trace(this ILog log, string message, Exception exception) { log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, log4net.Core.Level.Trace, message, exception); } public static void Trace(this ILog log, string message) { log.Trace(message, null); } public static void … Read more

Log4net randomly stops logging.

Log4Net will fail silently if something goes wrong and it is unable to write to its appenders. This is actually a good thing, since it means a bit of failed logging won’t bring down an otherwise healthy system, but it can be annoying when something isn’t logging as you expect. Your best bet is to … Read more

Maximum Filesize of LogFileAppender in Log4Net

The FileAppender class does not have the MaxFileSize/MaximumFileSize properties. You only get those if you use a RollingFileAppender. Here’s an example that will limit your file to a fixed maximum size, with no backups (set maxSizeRollBackups to 0). Note that when the file reaches its max size, it truncates (basically deletes all existing logging and … Read more

How to log SQL calls with NHibernate to the console of Visual Studio?

To show the SQL in the output window of Visual Studio, configure log4net to use TraceAppender in your log4net config. This: <appender name=”DebugSQL” type=”log4net.Appender.TraceAppender”> <layout type=”log4net.Layout.PatternLayout”> <conversionPattern value=”%date [%thread] %-5level %logger [%property{NDC}] – %message%newline” /> </layout> </appender> Then this: <logger name=”NHibernate.SQL” additivity=”false”> <level value=”DEBUG” /> <appender-ref ref=”DebugSQL” /> </logger> EDIT: I can’t seem to format … Read more

unable to get log4net working with .net windows service

Please note that when the process is run as Windows Service, Environment.CurrentDirectory will be “C:\Windows\system32” So if you put the log4net configuration file (log4net.config) next to your *.exe, you can use the following code to configure log4net. var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); XmlConfigurator.Configure(new FileInfo(Path.Combine(assemblyFolder, “log4net.config”)));

How do I use a GlobalContext property in a log4net appender name?

I ran into the same behavior and solved it by setting the global variable before calling the XmlConfigurator… Here is what I am successfully using: log4net.config details: <appender name=”RollingLogFileAppender” type=”log4net.Appender.RollingFileAppender,log4net”> <File type=”log4net.Util.PatternString” value=”App_Data/%property{LogName}” /> … </appender> Global.asax details: private static readonly log4net.ILog log = log4net.LogManager.GetLogger(“Global.asax”); void Application_Start(object sender, EventArgs e) { // Set logfile name … Read more

Log4Net – How to add a 2nd logger used only for specific sections of code

You can easily create a special logger like this: ILog specialLogger = LogManager.GetLogger(“SpecialLogger”); You can then configure the system to use a dedicated appender for this logger: <logger name=”SpecialLogger” additivity=”false”> <level value=”ALL” /> <appender-ref ref=”SpecialLogFileAppender” /> </logger> <root> <level value=”ALL” /> <appender-ref ref=”LogFileAppender” /> </root>

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)