Does anyone know if it\'s possible to access key/values from the appSettings section of a config file from the log4net config section in the same file (without using code)?<
You should work with log4net.Util.PatternString as the file type and refer to the app setting using %appSetting{SETTING}
See code below:
<appSettings>
<add key="Environment" value="DEV" />
<!-- etc -->
</appSettings>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString"
value="${APPDATA}\MyApp\%appSetting{Environment}\MyApp.log"/>
<!-- etc -->
</log4net>
Hope this still helps :)
If you look at my answer to this question:
Custom log4net property PatternLayoutConverter (with index)
you will find an example of a custom PatterLayoutConverter. In the case of the example, I called it: KeyLookupPatternConverter.
For convenience, here it is:
namespace Log4NetTest
{
class KeyLookupPatternConverter : PatternLayoutConverter
{
protected override void Convert(System.IO.TextWriter writer, LoggingEvent loggingEvent)
{
//Use the value in Option as a key into the "application settings" stored on the main form.
string setting;
if (Form1.AppSettings.TryGetValue(Option, out setting))
{
writer.Write(setting);
}
}
}
}
And here is how you would configure it:
//Log the "sessionid" and "userid" values from our "application settings" object
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p [session = %KLPC{sessionid}] [user = %KLPC{userid}] %m%n"/>
<converter>
<name value="KLPC" />
<type value="Log4NetTest.KeyLookupPatternConverter" />
</converter>
</layout>
In the configuration I am saying to get the "KLPC" setting from the Form's AppSettings.
In your case you want to get your values from the appSettings of the config file. It should be easy enough to modify the example I provided, replacing the code that retrieves the setting from the Form's AppSettings with code to retrieve the setting from the config file's app settings.
If you take this route, your AppSettingPatternConverter might look something like this (untested):
class AppSettingsPatternConverter : PatternLayoutConverter
{
protected override void Convert(System.IO.TextWriter writer, LoggingEvent loggingEvent)
{
//Use the value in Option as a key into the "application settings" stored on the main form.
string setting = ConfigurationManager.AppSettings[Option];
if (string.IsNullOrWhitespace(setting))
{
setting = string.Format("No setting value for key[{0}]", Option);
}
writer.Write(setting);
}
}
You would configure it like this:
//Log the "Name" setting from the application settings object
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p [name = %AppSetting{Name}] %m%n"/>
<converter>
<name value="AppSetting" />
<type value="Log4NetTest.AppSettingPatternConverter" />
</converter>
</layout>
You say that you want to do it without writing code. I'm not sure if it can be done, but using the approach that I show above is pretty common and pretty easy.
Thanks @wageoghe for your help. In the end, I decided to just add the environment value into the config file again (for simplicity):
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="${APPDATA}\MyApp\DEV\MyApp.log" />
<!-- etc -->
</log4net>