Question about log4net logging connection

info's Avatar

info

27 Sep, 2018 05:38 PM

To whom it may concern,

I have used diagnostics from postsharp with a logger of log4net.
I have configured log4net like this:

<log4net>
    <root>
        <level value="ERROR"/>
        <!--<appender-ref ref="LogFileAppender"/>
        <appender-ref ref="MongoDBAppender"/>-->
        <appender-ref ref="UdpAppender"/>
        <!--<appender-ref ref="AspNetTraceAppender"/>
        <appender-ref ref="DebugAppender"/>-->
    </root>
    <logger name ="Expedia.Mice.Core.BinaryCache" additivity="false">
        <level value="ALL" />
        <appender-ref ref="UdpAppender"/>
    </logger>

As you can see I want the root logger to only log ERRORs, but specific loggers to log at a different level (ALL, INFO, etc.).
Postsharp logging does NOT at all react to the log level setting in the named logger other than the root element.

The logger is configured like this in the startup of my app:

           LoggingServices.DefaultBackend = new Log4NetLoggingBackend();
           ((Log4NetLoggingBackend) LoggingServices.DefaultBackend).Options.GetLogger = ts => LogManager.GetLogger( ts.DisplayFullName );

If I enable logging with level ALL in root, I do get the different logger names, but I cannot control them independently.

GlobalAspects.cs is like this:
[assembly: Log(AttributeTargetTypeAttributes=MulticastAttributes.Private | MulticastAttributes.Protected | MulticastAttributes.Internal | MulticastAttributes.Public, AttributeTargetMemberAttributes=MulticastAttributes.Private | MulticastAttributes.Protected | MulticastAttributes.Internal | MulticastAttributes.Public)]

What can I do?

Thanks
  Andreas Kroll

  1. Support Staff 1 Posted by PostSharp Techn... on 05 Oct, 2018 03:19 PM

    PostSharp Technologies's Avatar

    Hello,

    I wasn't able to reproduce the problem. With the following log4net configuration:

    <log4net>
      <appender name="file" type="log4net.Appender.FileAppender">
        <file value="log4net.log" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
        </layout>
      </appender>
    
      <appender name="console" type="log4net.Appender.ColoredConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
      </appender>
      
      <root>
        <level value="ERROR"/>
        <appender-ref ref="console"/>
      </root>
      <logger name ="WithNamedLogger" additivity="false">
        <level value="ALL" />
        <appender-ref ref="console"/>
      </logger>
    </log4net>
    

    and the following test program:

    using log4net;
    using log4net.Config;
    using PostSharp.Patterns.Diagnostics;
    using PostSharp.Patterns.Diagnostics.Backends.Log4Net;
    using System.IO;
    
    class Program
    {
        static void Main(string[] args)
        {
            XmlConfigurator.Configure(new FileInfo("log4net.config"));
            LoggingServices.DefaultBackend = new Log4NetLoggingBackend();
            ((Log4NetLoggingBackend)LoggingServices.DefaultBackend).Options.GetLogger = ts =>
                LogManager.GetLogger(ts.DisplayFullName);
            WithRootLogger.Test();
            WithNamedLogger.Test();
        }
    }
    
    public class WithRootLogger
    {
        private static readonly Logger logger = Logger.GetLogger();
    
        public static void Test()
        {
            logger.Write(LogLevel.Debug, "root debug");
            logger.Write(LogLevel.Error, "root error");
        }
    }
    
    public class WithNamedLogger
    {
        private static readonly Logger logger = Logger.GetLogger();
    
        public static void Test()
        {
            logger.Write(LogLevel.Debug, "named debug");
            logger.Write(LogLevel.Error, "named error");
        }
    }
    

    My logging output is (as expected):

    2018-10-05 17:03:41,035 [1] ERROR WithRootLogger [(null)] - WithRootLogger | root error.
    2018-10-05 17:03:41,056 [1] DEBUG WithNamedLogger [(null)] - WithNamedLogger..cctor() | Starting.
    2018-10-05 17:03:41,057 [1] DEBUG WithNamedLogger [(null)] - WithNamedLogger..cctor() | Succeeded.
    2018-10-05 17:03:41,058 [1] DEBUG WithNamedLogger [(null)] - WithNamedLogger.Test() | Starting.
    2018-10-05 17:03:41,059 [1] DEBUG WithNamedLogger [(null)] -   WithNamedLogger | named debug.
    2018-10-05 17:03:41,060 [1] ERROR WithNamedLogger [(null)] -   WithNamedLogger | named error.
    2018-10-05 17:03:41,061 [1] DEBUG WithNamedLogger [(null)] - WithNamedLogger.Test() | Succeeded.
    Press any key to continue . . .
    

    Could you please provide more specific steps to reproduce the problem or tell us what is different in your setup?

    Regards,
    Roman

  2. Support Staff 2 Posted by PostSharp Techn... on 12 Oct, 2018 03:52 PM

    PostSharp Technologies's Avatar

    Hello,

    We are going to close this request as there have not been any further updates. Please feel free to reopen the discussion if you need more help.

    Thanks,
    PostSharp Team

  3. PostSharp Technologies closed this discussion on 12 Oct, 2018 03:52 PM.

  4. info re-opened this discussion on 15 Oct, 2018 04:56 PM

  5. 3 Posted by info on 15 Oct, 2018 04:56 PM

    info's Avatar

    Hi Roman,

    sorry for the late reply.
    I had to attend several urgent tasks and had no time to look into the logging matter again.

    I see your code and see the output, but it is rather different with what I am experiencing.

    First of all, I only get Entry/Exit Logging output if I set Log4net Root Logging Level to ALL.
    Everything above just does not output anything at all.

    Setting the Level to ALL outputs a lot of lines but stops after some time and after that no further output is written.
    If I reset IIS and then try again in works again.

    I will take some time to investigate further.
    I will include a custom logger and try to set breakpoints there to see if the logging is called at all.

    Thanks
      Andreas

  6. Support Staff 4 Posted by PostSharp Techn... on 17 Oct, 2018 09:02 AM

    PostSharp Technologies's Avatar

    Hi Andreas,

    you can find information about dealing with logging framework exceptions at http://doc.postsharp.net/6.0/logging-exception-handling. This should help you to figure out why the logging stops unexpectedly.

    Regarding the rest, we'll wait for you to give us more details on your issue, if that's OK.

    Best regards,
    -tony

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

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