0

I need to create per-user log - amount of logged data will be significant and I don't want to keep it in a single file.

Is there a way to configure log4net in such way, that I'll be able to specify, which file log should be written to?

Surely, I can write my own appender. But how to pass to it (nicely) information about the destination file?

Spook
  • 25,318
  • 18
  • 90
  • 167

1 Answers1

1

If your filename does not change during program execution, then you can initialise it

string logfileName = ...

log4net.GlobalContext.Properties["LogName"] = logfileName;

Note, this needs to be done before the first message is logged.

Then change your config file to use this property, eg

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="%property{LogName}" />
    ...

You can use the built in variables like ${USERNAME} and or ${COMPUTERNAME} in the pattern string .

eg

 <file type="log4net.Util.PatternString" value="LogFileFor_${USERNAME}_On_${COMPUTERNAME}.log" />
sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • Unfortunately, it *will* change during program execution, logging is being performed in the service, which works for different users simultaneously. – Spook May 28 '15 at 13:20