How to log Trace messages with log4net?

According to Rune’s suggestion I implemented a basic TraceListener which output to log4net: public class Log4netTraceListener : System.Diagnostics.TraceListener { private readonly log4net.ILog _log; public Log4netTraceListener() { _log = log4net.LogManager.GetLogger(“System.Diagnostics.Redirection”); } public Log4netTraceListener(log4net.ILog log) { _log = log; } public override void Write(string message) { if (_log != null) { _log.Debug(message); } } public override void … Read more

How can I change the file location programmatically?

log4net can handle this for you. Any appender property of type string can be formatted, in this case, using the log4net.Util.PatternString option handler. PatternString even supports the SpecialFolder enum which enables the following elegant config: <appender name=”LogFileAppender” type=”log4net.Appender.RollingFileAppender” > <file type=”log4net.Util.PatternString” value=”%envFolderPath{CommonApplicationData}\\test.txt” /> … </appender> Here’s a unit test that proofs the pudding: [Test] public … Read more

How to use log4net with Dependency Injection

I think you’re not seeing the forest for the trees here. ILog and LogManager are a lightweight façade almost 1:1 equivalent to Apache commons-logging, and do not actually couple your code to the remainder of log4net. <rant> I’ve also found that almost always when someone creates a MyCompanyLogger wrapper around log4net they miss the point … Read more

Dependency injection and named loggers

I’m using Ninject to resolve the current class name for the logger instance like this: kernel.Bind<ILogger>().To<NLogLogger>() .WithConstructorArgument(“currentClassName”, x => x.Request.ParentContext.Request.Service.FullName); The constructor of a NLog Implementation could look like this: public NLogLogger(string currentClassName) { _logger = LogManager.GetLogger(currentClassName); } This approach should work with other IOC containers as well, I guess.

Log4Net Logging of two different levels to two different appenders for the same logger

You should be able to set the threshold property of each appender separately and include them in the same root. <appender name=”filelogAppender” type=”log4net.Appender.RollingFileAppender”> <threshold value=”Error” /> </appender> <appender name=”dblogAppender” type=”log4net.Appender.AdoNetAppender”> <threshold value=”Info” /> </appender> <root> <appender-ref ref=”filelogAppender” /> <appender-ref ref=”dblogAppender” /> </root> reference

Correct way of using log4net (logger naming)

Regarding how you log messages within code, I would opt for the second approach: ILog log = LogManager.GetLogger(typeof(Bar)); log.Info(“message”); Where messages sent to the log above will be ‘named’ using the fully-qualifed type Bar, e.g. MyNamespace.Foo.Bar [INFO] message The advantage of this approach is that it is the de-facto standard for organising logging, it also … Read more

How to configure log4net programmatically from scratch (no config)

Here’s an example class that creates log4net config completely in code. I should mention that creating a logger via a static method is generally viewed as bad, but in my context, this is what I wanted. Regardless, you can carve up the code to meet your needs. using log4net; using log4net.Repository.Hierarchy; using log4net.Core; using log4net.Appender; … Read more

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