Unit tests fail when testing code with logging aspects

khorshid's Avatar

khorshid

08 Feb, 2018 09:57 PM

My project builds and run fine with the diagnostics aspects. But unit tests are failing with the following error:
System.InvalidOperationException : The logging back-end has not been initialized. Set the LoggingServices.DefaultBackend property before accessing a logged method

In the real app, the backend is defined at the point of entry. But for my unit test projects, this code is never executed. Adding the SkipPostSharp symbol to the test project didn't seem to do much either. What's the correct way to address this?

Thanks.

  1. Support Staff 1 Posted by PostSharp Techn... on 09 Feb, 2018 10:01 AM

    PostSharp Technologies's Avatar

    Hello,

    the correct way would be to disable logging in your unit tests, but there's a bug now (issue #15718), so the logging back-end is still required to be initialized even if the logging is disabled. This is going to be fixed in the next release of PostSharp 5.1.

    To make your unit tests working without logging, you can add this code you the unit tests project:

    static class ModuleInitializers
    {
        [ModuleInitializer(0)]
        public static void Initialize()
        {
            LoggingServices.DefaultBackend = new ConsoleLoggingBackend();
            LoggingServices.GetSource(LoggingRoles.Tracing).SetLevel(LogLevel.None);
        }
    }
    

    This code will be executed right after your unit tests assembly is loaded, so the logging will be disabled correctly.

    For more information about enabling and disabling logging, see http://doc.postsharp.net/log-enabling.
    For more information about the module initializers, see http://doc.postsharp.net/module-initializer.

    Best regards,
    -tony

  2. Support Staff 2 Posted by PostSharp Techn... on 09 Feb, 2018 10:08 AM

    PostSharp Technologies's Avatar

    You can use also

    static class ModuleInitializers
    {
        [ModuleInitializer(0)]
        public static void Initialize()
        {
            LoggingServices.DefaultBackend = NullLoggingBackend.Instance;
        }
    }
    

    -tony

  3. Support Staff 3 Posted by PostSharp Techn... on 20 Feb, 2018 03:55 PM

    PostSharp Technologies's Avatar

    Hello,

    We are going to close this request as we believe it was solved. Please feel free to reopen the discussion if you need more help.

    Best regards,
    PostSharp Team

  4. PostSharp Technologies closed this discussion on 20 Feb, 2018 03:55 PM.

  5. PostSharp Technologies re-opened this discussion on 21 Feb, 2018 06:00 PM

  6. Support Staff 4 Posted by PostSharp Techn... on 21 Feb, 2018 06:00 PM

    PostSharp Technologies's Avatar

    Hello,

    the bug #15718 has been fixed in the current preview release of PostSharp 5.1.4.
    Should you need further help with this issue, don't hesitate to re-open this discussion.

    Best regards,
    Alex

  7. PostSharp Technologies closed this discussion on 21 Feb, 2018 06:00 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac