将 App.config 应用到我的 DLL 程序集? [英] Apply an App.config to my DLL assembly?

查看:15
本文介绍了将 App.config 应用到我的 DLL 程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个类库作为抽象,用于登录我编写的任何应用程序、服务等.我通过使其非常可配置来满足我遇到的大多数应用程序/服务日志记录场景的需求,从而使其非常健壮.

配置旨在指定以下内容:

  • 要写入的日志级别
  • 为所有级别写入一个日志文件
  • 写入每个级别的单独文件
  • 记录截止(定期、应用事件、字节大小受限)
  • 日志文件过期(文件过期后删除日志文件)
  • 以纯文本或 XML 格式编写
  • 日志文件名格式规范
  • 是否在文件名前加上日期
  • 父应用的名称
  • 等等等等...

我已经阅读了一些关于 DLL 程序集配置的其他 stackoverflow 问题,它导致托管程序集/应用程序的 app.config 之间发生冲突.我相信我的程序集有理由提供一个配置文件.

这是适合那个场合的好场景吗?将我自己的配置烘焙到我的项目中以便我的记录器从 XML 文件中读取以检索配置值可能是一个更好的主意吗?

解决方案

你能做的是

  • 创建自定义配置部分(例如使用配置部分设计器工具)

  • 将程序集的配置放入单独的MyAssembly.config 文件

  • 从您的主机应用的配置中引用该程序集配置文件:

    <预><代码><配置><configSections><部分名称="您的程序集"type="YourAssembly.ConfigSection, YourAssembly"/></configSections><YourAssembly configSource="MyAssembly.config"/></配置>

那样,您可以将您的配置外部化"到一个单独的配置文件中,您只有一次(在您的程序集项目中),并且任何需要它的项目只需要在其自己的配置文件中进行这些设置.

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
  • 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. 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?

解决方案

What you could do is

  • create a custom configuration section (using e.g. the COnfiguration Section Designer tool)

  • put your assembly's configuration into a separate MyAssembly.config file

  • 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.

这篇关于将 App.config 应用到我的 DLL 程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆