简体   繁体   English

访问log4net包装器App.config的最佳方法

[英]Best way to access to log4net wrapper App.config

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. 就像使用log4net的许多人一样,我有自己的log4net 包装器 ,该包装器可将通过不同项目进行的所有日志集中化,并在每个项目中保存对log4net.dll的一些重复引用。

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. 这一点一起避免在我使用包装器的项目中不得不引用log4net.dll

I got it working for a simple console application, where I know where the App.config is located and its name: 我将其用于一个简单的控制台应用程序,该应用程序知道App.config的位置及其名称:

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. 每个项目都可以有自己的App.config。
  2. This file can change its name to {projectname}.config. 该文件可以将其名称更改为{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?) App.config文件的属性具有复制,嵌入等选项(但确切地说,它可以与其他App.config混合吗?)

In short: 简而言之:

What is the best way to access in a comfortable way the wrapper App.config wherever it is being used? 无论在哪里使用包装器App.config,以舒适的方式访问它的最佳方法是什么?

You get the config file name from AppDomain.CurrentDomain.SetupInformation.ConfigurationFile : 您可以从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. 例如,为每个应用程序,站点和计算机存储ASP.NET应用程序的配置数据,而为每个应用程序,用户和计算机存储可执行文件的配置数据。 Only the host knows the specifics of the configuration data for a particular circumstance. 仅主机知道特定情况下的配置数据的详细信息。

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();

There many ways to solve this issue, depending on the exact demands, in my case I created the log4net configuration programmatically like this: 有多种方法可以解决此问题,具体取决于确切的需求,在我的情况下,我以编程方式创建了log4net配置,如下所示:

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. 由于我的包装器是一个库,因此没有必要为该包装器库创建特定的app.config,因此可以将配置选项设置到将要使用log4net包装器的最终app.config文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM