Click here to Skip to main content
15,894,825 members
Articles / Programming Languages / C#
Tip/Trick

Isolated Storage using Log4Net

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
9 Jun 2013CPOL 14.4K   5  
IsolatedStorage with Log4Net

Introduction

This tip explains how to set the custom IsolatedStorage location for storing the log files when using the log4Net.

Background

Generally, in any application, we have a custom location where we keep the log files. However, if there is a business requirement that we need to have a isolated storage location (User Name specific), then the common configured location path will not be useful.

Like for example, I can have a static logger directory path configured in web.config / app.config file like this:

XML
<appSettings>
<add key="MyLoggerFile" value="C:\MyApplicationDirectory\SystemLog\MyLogFile.log"/>
<appSettings/>

What if we want a dynamic logger location specific to the user who has logged into the machine.

Something like this:

For Windows 7 Users:

C:\Users\TapasU\AppData\Local\MyProject\SystemLog\TapasU-MyLogFile.log

For Windows XP Users:

C:\Documents and Settings\TapasU\Local Settings\Application Data\
MyProject\SystemLog\TapasU-MyLogFile.log

If you are familiar with log4Net and want to log to such user specific location, then you need to add the following configSections in the web.config / app.config file.

Sample CS code to update the user specific log dorectory is given below:

Program.cs or Any .cs

C#
static void Main(string[] args)
{
	SetLoggerPath();
	log4net.ILog log = log4net.LogManager.GetLogger
	(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
	log4net.Config.XmlConfigurator.Configure();
	log.Debug("Log Me Started...");
	log.Debug("Log Me End...");
}

private static void SetLoggerPath()
{
// Creates the isolated storage
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore
(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
// Gets full path of isolated Storage
var fullIsolatedPath = isoStore.GetType().GetField
("m_RootDir", BindingFlags.NonPublic | 
BindingFlags.Instance).GetValue(isoStore).ToString();
// Modify the isolated storage path for application
log4net.GlobalContext.Properties["IsolatedStoragePath"] = 
fullIsolatedPath.Substring(0, 
fullIsolatedPath.LastIndexOf(@"\IsolatedStorage\"));
log4net.GlobalContext.Properties["UserName"] = Environment.UserName;
log4net.GlobalContext.Properties["loggedTime"] = 
DateTime.Now.Date.ToString("MMddyyyy");
}

Web.config / app.config

XML
<configuration>
<configSections>
<section name="log4net" 
type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="MyRollingFileAppender" />
</root>
<appender name="MyRollingFileAppender" 
type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" 
value="%property{IsolatedStoragePath}\
SystemLog\ErrLog-%property{UserName}-%property{loggedTime}.log"/>
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] 
%level %logger - %message%newline" />
</layout>
<rollingStyle value="Date" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
</appender>
</log4net>
<startup>
<supportedRuntime version="v4.0" 
sku=".NETFramework,Version=v4.0"/>
</startup>
<appSettings>
<add key="MyLoggerFile" 
value="C:\MyApplicationDirectory\SystemLog\MyLogFile.log"/>
</appSettings>
</configuration>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --