开发者

Logging part specific error messages for mef parts

开发者 https://www.devze.com 2023-03-08 06:11 出处:网络
What is a preferred approach for logging part specific errors with imported parts?E.G. if you have the following contract:

What is a preferred approach for logging part specific errors with imported parts? E.G. if you have the following contract:

public interface IDoStuff
{
  void DoYourStuff();
}

with multiple implementations:

[Export(typeof(IDoStuff))]
public class DoStuffCorrectly : IDoStuff
{    
    //implement void run
}

[Export(typeof(IDOstuff))]
public class DoStuffWithExceptions : IDoStuff
{
//  implement void run and throws exception
}

and you have a Type that uses mef to compose the parts.

public class DoStuffRunner
{  
  [ImportMany(typeof(IDoStuff))]
  IEnumerable<IDoStuff> DoStuffPats {get;set;}

  //some method that loops through the IEnumerable and calls run
  public void Run()
  {
    foreach(IDostuff doit in DoStuffParts)
    {
      doit.Run();
    }
  }
 }

In the executing assembly with the importer I am using entlib Exception Handling and Logging Application blocks. The logging application block is configured to send general error messag开发者_运维问答es to the team. Some of the information that I would like to be able to include is which part failed, and possibly which group gets the email.

This is simple enough to configure statically in the app config, but would lead to a 1:1 configuration for each part that is added and kind defeats the purpose of dropping the dll in the bin. It would be neat if you could control the configuration in the part's assembly.

So, what are some possible approaches that would allow a part to expose information that could allow an imported part to provide logging configuration information that would jive with the MEF ideology?


The MEF way is to import a logging interface into your plugins and/or export whatever metadata you need to configure your logger by using custom attributes on your exported class.

I'm not really familiar enough with that logging library (we use log4net) to know what metadata you need or how logging would be actuated in a unified manner if you didn't import a logging interface interface.

0

精彩评论

暂无评论...
验证码 换一张
取 消