Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue logging with Configuration created from code with version 1.0.0 (works in 0.1.32.1) #6

Open
GertHermansOrendes opened this issue Aug 20, 2020 · 2 comments

Comments

@GertHermansOrendes
Copy link

LoggingIssue.zip
The attached dummy project shows an issue with slf4net.log4net v1.0.0 nuget package. Running this code with version 0.1.32.1 works fine. The file is created and the logged line is there. When upgrading to version 1.0.0 and deleting the file (../ProgramData/LoggingTesting/LisLogging_{Date}.txt) and then running it again. The log file is created, but the logged line is not present anymore.

@adrianluisgonzalez
Copy link
Member

Hi,

The issue is version 0.1.32 was using deprecated log4net methods to set up configuration without a repository. There is now an slf4net repository imaginatively named slf4net-repository to provide isolation.

Two options:

  1. Alias your assembly to use the slf4net-repository like this:
[assembly: AliasRepository("slf4net-repository")]
  1. Use the slf4net-repository name and change the ordering so the Log4NnetLoggerFactory initializes first:
    static void Main(string[] args)
    {
        //  Create log4net ILoggerFactory and set the resolver
        var factory = new slf4net.log4net.Log4netLoggerFactory();
        var resolver = new SimpleFactoryResolver(factory);
        slf4net.LoggerFactory.SetFactoryResolver(resolver);
        var log = slf4net.LoggerFactory.GetLogger(typeof(Program));
        
        var layout = new PatternLayout
        {
            ConversionPattern = "%d{ABSOLUTE}: %message %newline"
        };
        layout.ActivateOptions();
        var fileAppender = new RollingFileAppender();
        fileAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;
        fileAppender.Layout = layout;
        var path = GetLoggingPath();
        fileAppender.File = path + System.IO.Path.DirectorySeparatorChar + "LISlogging_.txt";
        fileAppender.AppendToFile = true;
        fileAppender.PreserveLogFileNameExtension = true;
        fileAppender.StaticLogFileName = false;
        fileAppender.DatePattern = "yyyy-MM-dd";
        fileAppender.MaxSizeRollBackups = 10;
        fileAppender.ActivateOptions();
        ILoggerRepository repository = log4net.LogManager.GetRepository(slf4net.log4net.Log4netLoggerFactory.SLF4NET_REPOSITORY);
        BasicConfigurator.Configure(repository, fileAppender);
        var root = (repository as Hierarchy)?.Root;
        if (root == null) return;
        root.Level = log4net.Core.Level.All;
        // trigger logging
        log.Info("log this line");
    }
}

Honestly, we weren't really expecting this usage pattern so it could be better. Being able to set/confgure the repository name used by slf4net and to also check first if it already exists could be useful enhancements.

@GertHermansOrendes
Copy link
Author

Hi Thanks for the answer.
I our project the logs come from some library we wrote that uses slf4net. The software that uses this library creates the fileappender to get the logs. Getting the logger to send the logs is happening in the library so we can not use this solution.
I did find a work around though (maybe not the cleanest one but it works)
I've put it on stackoverflow if someone does the same things as us:
https://stackoverflow.com/questions/63517288/no-logging-with-configuration-made-in-code-using-latest-version-of-slf4net-1-0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants