...">

Dynamic username log4net?

Let's say I have 3 smtp applications in one 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 on the server, I want to start the log. In the case of a development server, it will write a log from "emailDevelopment". I have a system variable on each server named "ApplicationEnvironment" whose value is "Development", "Beta", "Production" based on the server names. Now I can still configure root in log4net so that it triggers email based on the server name.

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

      

+2


a source to share


2 answers


This doesn't directly answer your question, but another approach is to just have multiple log4net config files and call XmlConfigurator.Configure()

on the correct one. For example, you may have Logging.Development.Config

, Logging.Beta.Config

etc.

Somewhere in the code, you define the "environment" and configure with the file you want.



I even went so far as to have multiple config files and output different parts of them into one XML representing the "true" config, and then call the method Configure()

. For example, Logging.Appenders.Config

one that has all applications and takes all of them and combines it with one of your environment-specific config files above; the environment-specific ones just reference what they need and the rest are effectively inactive / not tied to that environment.

+4


a source


Even after I only wrote the XSD file for the log4net config , I still don't know how to do it.

You might be able to do something like:

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

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

      

You can now reference this value in log format: "% property {host}"



To perform filtering, you will need to use the 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 might even be a built-in property that you could use and that should work. See also this post: http://geekswithblogs.net/rgupta/archive/2009/03/03/dynamic-log-filenames-with-log4net.aspx

For me, me and me ... I would have approached him differently. I would get my own default SMTP application and in the ActivateOptions () method I would adjust the values ​​according to the environment. This will allow you to use one SMTP application with consistent rules and still provide three public properties for each of the email addresses you want to send. It's not difficult, try it!

+1


a source







All Articles