Reputation: 1084
As many people using log4net I have my own log4net wrapper that centralizes all the logging through different projects, also saves some repeated references to log4net.dll in each project.
I am using this solution posted here. Together with this one to avoid having to reference log4net.dll in projects where I use my wrapper.
I got it working for a simple console application, where I know where the App.config is located and its name:
FileInfo configFileInfo = new FileInfo("App.config");
log4net.Config.XmlConfigurator.ConfigureAndWatch(configFileInfo);
I see potential problems here:
In short:
What is the best way to access in a comfortable way the wrapper App.config wherever it is being used?
Upvotes: 0
Views: 669
Reputation: 1084
There many ways to solve this issue, depending on the exact demands, in my case I created the log4net configuration programmatically like this:
var hierarchy = (Hierarchy)log4net.LogManager.GetRepository();
var patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
var console = new ConsoleAppender();
console.Layout = patternLayout;
var roller = new RollingFileAppender();
roller.AppendToFile = true;
roller.File = Path.Combine(Logger.LogPath, "log.txt");
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 5;
roller.MaximumFileSize = "100MB";
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.StaticLogFileName = true;
roller.ActivateOptions();
hierarchy.Root.AddAppender(console);
hierarchy.Root.AddAppender(roller);
MemoryAppender memory = new MemoryAppender();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
hierarchy.Root.Level = Level.Debug;
hierarchy.Configured = true;
Another approach could be to embed it as suggested in one of the comments.
As my wrapper is a library, makes no sense to create a specific app.config for that wrapper-library so the configuration options can be set up into the final app.config file where the log4net wrapper is going to be used.
Upvotes: 0
Reputation: 73313
You get the config file name from AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
:
The configuration file describes the search rules and configuration data for the application domain. The host that creates the application domain is responsible for supplying this data because the meaningful values vary from situation to situation.
For example, the configuration data for ASP.NET applications is stored for each application, site, and computer, while the configuration data for an executable is stored for each application, user, and computer. Only the host knows the specifics of the configuration data for a particular circumstance.
Upvotes: 1
Reputation: 27944
You can get the name of the executing file which gives you the name of the config file. Something like:
Assembly.GetCallingAssembly();
or
Assembly.GetExecutingAssembly();
Upvotes: 0