简体   繁体   English

将App.config应用于我的DLL程序集?

[英]Apply an App.config to my DLL assembly?

I'm writing a class library as an abstraction to use for logging in any application, service, etc. that I write. 我正在编写一个类库作为抽象,用于登录我编写的任何应用程序,服务等。 I'm making it decently robust by making it very configurable to suit my needs for most application/service logging scenarios that I come across. 我通过使其非常可配置以满足我对大多数应用程序/服务日志记录方案的需求而使其变得非常强大。

The config is designed to specify things such as: 配置旨在指定以下内容:

  • What logging level to write 要写入什么日志记录级别
  • Write to one log file for all levels 写入所有级别的一个日志文件
  • Write to separate files per level 写入每个级别的单独文件
  • Logging cutoff (periodic, app event, byte size restricted) 记录截止(周期性,应用事件,字节大小受限)
  • Log file expiration (delete log files after file age) 日志文件到期(文件年龄后删除日志文件)
  • Write as flat text or XML 写为平面文本或XML
  • Log file name format specification 日志文件名格式规范
  • Whether to prefix filename with date 是否在文件名前加上日期
  • Parent app's name 父应用程序的名称
  • etc, etc, etc... 等,等等......

I've read some other stackoverflow questions regarding configs for DLL assemblies and it causing conflict between the app.config for the hosting assembly/app. 我已经阅读了一些关于DLL程序集配置的其他stackoverflow问题,它导致了托管程序集/ app的app.config之间的冲突。 I believe that my assembly has just cause to provide a config file. 我相信我的程序集只是提供配置文件。

Is this a good scenario for that occasion? 这是一个很好的场景吗? Is it perhaps a better idea to bake my own config into my project so that my logger reads from XML files to retrieve config values? 将我自己的配置烘焙到我的项目中可能是一个更好的主意,以便我的记录器从XML文件读取以检索配置值?

What you could do is 你能做的是

  • create a custom configuration section (using eg the COnfiguration Section Designer tool) 创建自定义配置部分(使用例如COnfiguration Section Designer工具)

  • put your assembly's configuration into a separate MyAssembly.config file 将程序集的配置放入单独的MyAssembly.config文件中

  • reference that assembly config file from your host app's config: 从主机应用程序的配置引用程序集配置文件:

     <configuration> <configSections> <section name="YourAssembly" type="YourAssembly.ConfigSection, YourAssembly" /> </configSections> <YourAssembly configSource="MyAssembly.config" /> </configuration> 

That way, you can "externalize" your configuration into a separate config file which you have only once (in your assembly's project), and any project needing it just needs those settings in its own config file. 这样,您可以将配置“外部化”为一个单独的配置文件,您只有一次(在程序集的项目中),任何需要它的项目只需要在自己的配置文件中进行这些设置。

Sounds like a custom config section would work well in your case. 听起来像自定义配置部分将适用于您的情况。 Many libraries, such as the Enterprise Library do exactly this. 许多库,例如企业库就是这样做的。 Check out the MSDN article about creating one. 查看MSDN有关创建一篇文章的文章

The .NET config mechanism is not meant to handle configuration files for DLLs. .NET配置机制不是为了处理DLL的配置文件。 You should configure your application with appropriate settings and pass them on to the class you are instantiating from the DLL. 您应该使用适当的设置配置应用程序,并将它们传递给您从DLL实例化的类。


It is possible to add settings to a DLL project as you'd usually do for applications. 可以像通常为应用程序一样向DLL项目添加设置。 All you then need to do is copy the relevant sections and entries into the application's app.config manually and it will work. 您需要做的就是手动将相关的部分和条目复制到应用程序的app.config中,它将起作用。

It is, however, still true that there's no point copying the DLL's config file. 但是,仍然没有必要复制DLL的配置文件。 It will not be read. 它不会被阅读。

Another mechanism is to have a seperate configuration file (*.dll.config) for your assembly. 另一种机制是为程序集配置一个单独的配置文件(* .dll.config)。 The technique is shown here: http://blog.rodhowarth.com/2009/07/how-to-use-appconfig-file-in-dll-plugin.html 该技术如下所示: http//blog.rodhowarth.com/2009/07/how-to-use-appconfig-file-in-dll-plugin.html

The above, imitate the standard app.config technique for assemblies. 以上,模仿程序集的标准app.config技术。

In my opinion the dll configuration reading code should only exist in the corresponding dll and in a seperate class - with single responsibility of reading configuration entries from the *.dll.config. 在我看来,dll配置读取代码应该只存在于相应的dll和单独的类中 - 单独负责从* .dll.config中读取配置条目。 This is a nice way of having configuration file for an assembly in a way similar to the configuration file (app.config) an executable can have. 这是一种以类似于可执行文件可以具有的配置文件(app.config)的方式为程序集配置文件的好方法。

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

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