开发者

dynamic log4net appender name?

Let's say i have 3 smtp appenders in same log4net file whose names are:

<appender name = "emailDevelopment".. />
<appender name = "emailBeta".. />
<appender name = "emailProduction".. />

Let's say i have 3 different servers(Dev, Beta, Production). Depending upon the server, i want to fire the log. In case of Development server, it would fire log from "emailDevelopment". I have a system variable in each server named "ApplicationEnvironment" whose value is Development, Beta, Production based on the server names. Now is there anyway i can setup root in开发者_Go百科 log4net so that it fires email depending upon the server name.

<root>
      <priority value="ALL" />         
      <appender-ref ref="email<environment name from whose appender should be used>" />      
</root>


This doesn't directly answer your question, but another approach is to simply have multiple log4net configuration files and call XmlConfigurator.Configure() on the right one. For example, you might have Logging.Development.Config, Logging.Beta.Config and so on.

Somewhere in code, you determine the "environment" and configure using the file you want.

I've even gone so far as to have multiple config files and pull different parts of them out into a single XML representing the "true" config, and then calling the Configure() method on that. For example, Logging.Appenders.Config which has all the appenders, and takes all of them and combines it with one of your environment-specific config files above; the environment-specific ones simply reference what they need, and the rest are effectively inactive/unreferenced for that environment.


Even after having written the only XSD file for log4net configuration I'm still not aware of an easy way to achieve this.

You might be able to do something like:

log4net.GlobalContext.Properties["host"] = new ClassThatToStringsHost();

class ClassThatToStringsHost 
{ public override string ToString() { return "whatever"; } }

Now you can reference this value from the Log format with: "%property{host}"

To perform the filtering you will need to use a filter configuration in the adapter(s):

<appender name="file" type="log4net.Appender.RollingFileAppender">
  <filter type="log4net.Filter.PropertyFilter">
    <Key value="host" />
    <StringToMatch value="whatever" />
  </filter>

  <!-- Anything not accepted by the above should be excluded -->
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>

There may even be a built-in property you could leverage and this should work. See also this post: http://geekswithblogs.net/rgupta/archive/2009/03/03/dynamic-log-filenames-with-log4net.aspx

For me, myself, and I... I would approach it another way all together. I would derive my own SMTP appender from the default and in the ActivateOptions() method I'd configure the values according to the environment. This would allow you to use one SMTP appender with consistent rules and yet provide three public properties for each of the email addresses you want to send from. It's not hard, give it a try!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜