Efrain
Efrain

Reputation: 3364

log4net wrapper doesn't log (during MSTest UTs)

I have a wrapper class for log4net to log across multiple classes and assemblies within a group of dll-libraries. (This may be poor design, but.. don't ask) The code basically looks like this:

public static class Logger
{
    private static ILog log;

    static Logger()
    {
        log = LogManager.GetLogger("abcd");
        XmlConfigurator.Configure(new FileInfo("LoggerConfig.log4net"));
        log.Info("=== NEW SESSION ===");
    }

    public static void Log(string message)
    {
        log.Debug(message);
    }
}

Now.. if the static constructor was the static main routine of a simple executable, this works perfectly fine (I quickly made a console app to test it). But here it does not. Since I deal with standalone assemblies, not executables, the only way I can easily test the logger is through MSTest. This may be a cause for this problem. I am not 100% sure if it maybe simply can't find the config file. It is copied at compilation and lies in the same folder as the assembly containing the logger class, so i think that should work.

bleh.. stuck since three hours. :T any suggestions?

Upvotes: 2

Views: 1727

Answers (4)

Daniel Elliott
Daniel Elliott

Reputation: 22857

MsTest shadow copies things all over the place and as peer and Hinek say the location of the file is probably the issue. I find including files within the test assembly is one way to get around this.

Check out a previous answer here for details.

Upvotes: 0

Peter
Peter

Reputation: 27944

My psychic debugger says that your LoggerConfig.log4net is not in the right directory. It is not deployed to your test directory and not found when running the test. That is why there is no log output. Deploy the file in the test directory and you will have you logging output.

EDIT: More precisely, you need to add it as a deployment file under Test->Edit Test Settings->>Deployment (as described here: How can I get "Copy to Output Directory" to work with Unit Tests? )

Upvotes: 5

San
San

Reputation: 928

Log4net uses configs in App.config? Try to put them also in the testlibrary?

I know the problem by Application with many libraries and there de config must only be in the Main-Application.

Upvotes: 0

Hinek
Hinek

Reputation: 9729

Have you tried to reference the config file by an absolute path? Maybe the root directory for MSTests is different to the Console App root dir?

Upvotes: 0

Related Questions