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

Jef*_*Fay 8 c# dll configuration

我正在编写一个类库作为抽象,用于登录我编写的任何应用程序,服务等.我通过使其非常可配置以满足我对大多数应用程序/服务日志记录方案的需求而使其变得非常强大.

配置旨在指定以下内容:

  • 要写入什么日志记录级别
  • 写入所有级别的一个日志文件
  • 写入每个级别的单独文件
  • 记录截止(周期性,应用事件,字节大小受限)
  • 日志文件到期(文件年龄后删除日志文件)
  • 写为平面文本或XML
  • 日志文件名格式规范
  • 是否在文件名前加上日期
  • 父应用程序的名称
  • 等,等等......

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

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

mar*_*c_s 9

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>
    
    Run Code Online (Sandbox Code Playgroud)

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.