質問

I am trying to figure out how to use the ExtendedLog4NetLogger.cs to change the log file path dynamically at runtime or using the LoggingFacility?

This should be something similar to using log4net directly like this:

log4net.GlobalContext.Properties["LogName"] = logName; 

How would I access the ExtendedLogger if I register log4net integration like this:

container.AddFacility<LoggingFacility>(f => f.UseLog4Net());

Update: I use the following code to register the Extended Logger

container.AddFacility<LoggingFacility>(LoggerImplementation.ExtendedLog4net).WithConfig(configFile).ToLog(Lo‌gger)); 

I get no runtime exceptions and the logger is not a null instance but I don't see the log file created at all using the global properties, I also set the config value to this for the appender:

<file type="log4net.Util.PatternString" value="%property{LogName}" />

If I just set the file property in the config file to full path it does work. I am wondering if it is not working because configuration is done before setting the global variable.

役に立ちましたか?

解決

extendedlogger.GlobalProperties["logName"] = logName;

To enable extended logger you need to do:

container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.ExtendedLog4net));

他のヒント

1. How would I access the ExtendedLogger if I register log4net integration like this:

Using the dependency injection you can expect IExtendedLogger object in the place where you need it.

2. I am wondering if it is not working because configuration is done before setting the global variable.

That's right. You need to reconfigure log4net after setting a property.

Here is an example:

using Castle.Core.Logging;
using log4net.Config;

class MyClass {
    private readonly IExtendedLogger _extendedLogger;

    public MyClass(IExtendedLogger extendedLogger) {
        _extendedLogger = extendedLogger;
    }

    public void MyFunction() {
        _extendedlogger.GlobalProperties["logName"] = logName;
        XmlConfigurator.Configure();
        _extendedlogger.Error("my error message");
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top