2

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?

Efrain
  • 3,248
  • 4
  • 34
  • 61

4 Answers4

5

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? )

Community
  • 1
  • 1
Peter
  • 27,590
  • 8
  • 64
  • 84
  • mmm.. you're right, I was wondering about those 'Out' folders in the TestResults subfolders - there the file is not present. My concern however is that I want it to 'belong' to the assembly/project in which the logger is, since in the end, i want to deploy the library without the unit test assemblies. Is there a way to tell MSTest to copy this file? – Efrain Oct 18 '11 at 09:34
  • I'm not sure, but I think you can add the file to deployment files. But in the case of unit test your config file has to come out of the unit test project (otherwise if you change your logging settings your unit tests will fail). I would try to add the file and mark is for deployment. – Peter Oct 18 '11 at 09:38
  • Ah, one can add it as a deployment file under Test->Edit Test Settings->>Deployment (as described here: http://stackoverflow.com/questions/227545/how-can-i-get-copy-to-output-directory-to-work-with-unit-tests ) – Efrain Oct 18 '11 at 09:55
0

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?

Hinek
  • 9,519
  • 12
  • 52
  • 74
0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
  • I guess this will work.. but I don't want to do that, since the library user shall be able to pick the logging location and level after deployment. (see comment above) – Efrain Oct 18 '11 at 09:38
0

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.

San
  • 868
  • 1
  • 9
  • 18