Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable file logging for log4net from code instead of from configuration

Tags:

c#

.net

log4net

Why in the world does the following test fail? (its in xunit) I've tried it with different appenders and it never writes anything though the log seems like it is ready to write. I eventually created my own appender just to test it.

    public class TestAppender : AppenderSkeleton {
        public event Action<LoggingEvent> AppendCalled = delegate { };
        protected override void Append(LoggingEvent loggingEvent) {
            AppendCalled(loggingEvent);
        }
    }
    public class Class1 {
        private TestAppender _appender = new TestAppender();
        public Class1() {
            log4net.Util.LogLog.InternalDebugging = true;
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
            Logger rootLogger = hierarchy.Root;
            rootLogger.Level = Level.All;
            Logger coreLogger = hierarchy.GetLogger("abc") as Logger;
            coreLogger.Level = Level.All;

            coreLogger.Parent = rootLogger;
            PatternLayout patternLayout = new PatternLayout();
            patternLayout.ConversionPattern = "%logger - %message %newline";
            patternLayout.ActivateOptions();
            _appender.Layout = patternLayout;
            _appender.ActivateOptions();
            coreLogger.AddAppender(_appender);            
        }
        [Fact]
        public void Test() {
            bool called = false;
            _appender.AppendCalled += e => called = true;
            var log = LogManager.GetLogger("abc");
            log.Debug("This is a debugging message");
            Thread.Sleep(TimeSpan.FromSeconds(2));
            log.Info("This is an info message");
            Thread.Sleep(TimeSpan.FromSeconds(2));
            log.Warn("This is a warning message");
            Thread.Sleep(TimeSpan.FromSeconds(2));
            log.Error("This is an error message");
            Assert.True(called);
        }
}
like image 645
George Mauer Avatar asked Sep 17 '09 04:09

George Mauer


People also ask

How do I add log4net to app config?

Now, add the section "<log4net></log4net>" after the <configSections/> element in your app. config file. Next, inside the "<log4net></log4net>" section, place the configuration details as shown in the code snippet given below. That's all you need to do to configure log4net.

Where is log file log4net?

The log4net. config file is located in the \Enterprise7\bin\ directory and is the configuration file for the log4net logging application.


2 Answers

I always use the code below to configure log4net from code. Works great!

Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders(); /*Remove any other appenders*/

FileAppender fileAppender = new FileAppender();
fileAppender.AppendToFile = true;
fileAppender.LockingModel = new FileAppender.MinimalLock();
fileAppender.File = Server.MapPath("/") + "log.txt";
PatternLayout pl = new PatternLayout();
pl.ConversionPattern = "%d [%2%t] %-5p [%-10c]   %m%n%n";
pl.ActivateOptions();
fileAppender.Layout = pl;
fileAppender.ActivateOptions();

log4net.Config.BasicConfigurator.Configure(fileAppender);

//Test logger
ILog log =LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Debug("Testing!");
like image 85
Njal Gjermundshaug Avatar answered Oct 06 '22 14:10

Njal Gjermundshaug


Just throwing a guess out there...

Do you have the XmlConfiguration defined in your assembly?

Did you forget it in your test project?

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

That usually burns me every once in a while.

like image 39
Ty. Avatar answered Oct 06 '22 13:10

Ty.