I'm having the worst time trying to configure log4net properly. First, I need to give some background about how I've been using it so far (and successfully).
My application has a log4net configuration file that defines the FileAppender that it should log all data to. All of my assemblies have an ILog
member variable, and set it to LogManager.GetLogger( typeof( myclass))
, where myclass is the class that is logging.
Everything works great like this.
Now I also have an assembly that is used slightly differently. It can be instantiated multiple times, and the requirement is that each instance logs to its own file. I was able to get this to work programmatically, and ditching the logging.xml file.
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.Level = log4net.Core.Level.All;
hierarchy.Configured = true;
string module_path = MyCompany.Shared.Utils.FileSystem.GetModulePath();
string folder = module_path + "\\logs\\";
string path = String.Format("{0}{1}-log.txt", folder, name);
_log = LogManager.GetLogger( name);
var fa = new log4net.Appender.FileAppender() { Name = name, File = path, AppendToFile = true};
var layout = new PatternLayout { ConversionPattern = "%date{{yyyy-MM-dd-HH_mm_ss.fff}} [%thread] %-5level %logger - %message%newline" };
layout.ActivateOptions();
fa.Layout = layout;
fa.ActivateOptions();
((Logger)_log.Logger).AddAppender( fa);
This also works great. Now here's where the problem comes in: the above assembly uses another assembly that also uses log4net. However, that assembly is expecting there to be a logger already configured for use, and apparently the way I'm setting up log4net programmatically is preventing that from working.
I have enabled internal log4net debugging, and the error message I see is pretty explicit about the error, but I'm not sure how to (best) solve the problem:
log4net: Logger: No appenders could be found for logger [test_logger] repository [log4net-default-repository]
log4net: Logger: Please initialize the log4net system properly.
log4net: Logger: Current AppDomain context information:
log4net: Logger: BaseDirectory : C:\Sample\bin\Debug\
log4net: Logger: FriendlyName : SampleTestApp.vshost.exe
log4net: Logger: DynamicDirectory:
log4net: Hierarchy: Shutdown called on Hierarchy [log4net-default-repository]
I assume that the logger is created, since I get the same error even if I explicitly use the LoggerFactory
to create a "test_logger" logger. So it's just a matter of figuring out how to create the appender for that logger. I have tried to use the previous code to create a FileAppender
for this logger, but even though the code runs, I get the same error.
Can anyone suggest things for me to try to get this to work?
EDIT -- I should clarify that I really would like all of the log messages to go into the FileAppender that was created by the test app. I would like to avoid having separate files (one for each assembly).