We have a ASP .Net application whereby we use Log4Net to log details within the app – nothing new there – and the rolling log filenames are in the usual format of:
rolling-log.txt rolling-log.txt.1 rolling-log.txt.2 etc.
A each user of the application adds to the logfile, the logfile can be difficult to read for a specific user’s case and so, we’d like to modify the config file somehow to record the user’s log details individually, each writing to a specific file, e.g.
<applicationId>rolling-log.txt <applicationId>rolling-log.txt.1 <applicationId>rolling-log.txt.2 etc.
where is each user’s unique application Id, made up of a five digit number, e.g.
12345rolling-log.txt
Any ideas on the best way to implement this, assuming that it’s possible?
Cheers
Brett
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
<file type="log4net.Util.PatternString">
<conversionPattern value="C:Logslog-%date{ yyyy.MM.dd.HH.mm.ss}-[%processid].log" />
</file>
Method 2
look for log4net Context Properties…
in your code :
log4net.GlobalContext.Properties["id"] = "12345";
then
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("configPath"));
in the log4net config file:
<file type="log4net.Util.PatternString"
value="%property{id}rolling-log.txt" />
Method 3
I believe the Log4Net configuration file supports environment variables (e.g. USERNAME) as well as customizable patterns which should give you what you want.
Look at “PatternString for pattern based configuration” in the Log4Net V1.2.10 release notes.
Method 4
I don’t think that log file per user is possible but you can write custom layer between log4net and your application which prepends user id before writing to log.
Method 5
You can write a custom layout by inherit from XmlLayoutBase.
Method 6
Checkout the RollingPatternFileAppender, just like the rollingfileappender plus dynamic file name
http://mysite.verizon.net/vze14bhji
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0