1

I am using log4net in an UWP app.

my config file is the following:

<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="false">
    <appender name="Console" type="log4net.Appender.DebugAppender">
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date - %message%newline" />
        </layout>
    </appender>

    <appender name="LocalStorageFile" type="MyProject.LocalStorageFileAppender, MyProject">
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date - %message%newline" />
        </layout>
    </appender>

    <root>
        <level value="INFO" />
        <appender-ref ref="Console" />
        <appender-ref ref="LocalStorageFile" />
    </root>
</log4net>

Since this is an UWP app, I need to manually load the XML file with the following method:

private static async Task ConfigureLog4Net()
{
    // config for log4net:
    Uri uri = new Uri("ms-appx:///log4netConfig.xml");
    StorageFile sampleFile = await StorageFile.GetFileFromApplicationUriAsync(uri);
    var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
    using (Stream s = await sampleFile.OpenStreamForReadAsync())
    {
        log4net.Config.XmlConfigurator.Configure(logRepository, s);
    }
    Logger log = new Logger(typeof(AppConfigReader));
    log.InfoFormat("Log is configured");
}

However, in my log there is always the fully qualified name of the logger, like this example:

MyCorp.Controls.MyView:2019-01-23 10:38:32,434 - log test here

How can I configure log4net to see a log like this instead (without the logger name):

2019-01-23 10:38:32,434 - log test here
Cristiano Ghersi
  • 1,944
  • 1
  • 20
  • 46
  • I suspect it isn't loading your configuration. Test this by inserting a bunch of garbage into the XML. Where is your XML configuration located relative to the project? Can you share more of the configuration as well? –  Jan 23 '19 at 19:31
  • I updated the question with the relevant code. Note: if I update the conversionPattern in the XML e.g. to "%date - %message%newline", I can see this change reflected in the logs. – Cristiano Ghersi Jan 23 '19 at 20:01
  • I checked its source code, It should be default behavior. See https://github.com/apache/logging-log4net/blob/master/src/Core/LogImpl.cs#L414 If you do not want this format, you need to change its source code and compile a custom version for your project. – Bite Jan 24 '19 at 08:41
  • @Bite `If you do not want this format, you need to change its source code and compile a custom version for your project` Uh, no, log4net is configurable. I'm guessing you have never used a logging framework. It would be useless if you couldn't change the defaults! –  Jan 24 '19 at 14:02
  • @Amy how can I configure correctly my app then? – Cristiano Ghersi Jan 24 '19 at 21:36
  • I don't know, I see nothing immediately wrong with your code, but its been a few years since I last used log4net. If I knew the answer to that, I would have answered your question. –  Jan 24 '19 at 21:36
  • @Bite You don't do that. You use configuration to change how log4net behaves. In this case, it is the appenders, not the loggers, that needs to be configured. This is central to any modern .Net logging framework's architecture. Please follow any log4net tutorial, or any NLog tutorial. Again, it is *completely* unnecessary to use inheritance or modify the source code of log4net to achieve a change in how the appender's work. –  Jan 25 '19 at 02:31
  • @CristianoGhersi Can you add your entire log4net configuration to the question? It's apparently ignoring your appender and using a default. –  Jan 25 '19 at 02:36
  • @Amy I added the full content of the Log4net config file – Cristiano Ghersi Jan 28 '19 at 16:25

0 Answers0