0

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:

  1. Each project can have its own App.config.
  2. This file can change its name to {projectname}.config. (in release perhaps?)
  3. App.config file properties have options like copy, embedded, etc. (but where exactly, can it get mixed up with other App.config?)

In short:

What is the best way to access in a comfortable way the wrapper App.config wherever it is being used?

Community
  • 1
  • 1
juagicre
  • 1,065
  • 30
  • 42

3 Answers3

1

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.

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Hi @stuartd Thanks for the answer. Does it make sense for a library project to have its own app config? Would be cleaner if embedded? – juagicre May 14 '16 at 21:54
  • 1
    Sounds like you should move your log4net config out of the app config into a standalone file then. You can embed the log4net config file in the project and use the overload of Configure that takes a stream, or you could just store it as a static string somewhere. – stuartd May 15 '16 at 18:13
0

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();
Peter
  • 27,590
  • 8
  • 64
  • 84
  • Is this the best way? It sounds to me like very tricky... finally maybe is better to break down all and include the log4net.dll – juagicre May 14 '16 at 16:32
0

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.

juagicre
  • 1,065
  • 30
  • 42