How to write a log on an ASP.NET website virtual directory?

1.4k views Asked by At

My website is running as an ASP.NET user in IIS. I've created one virtual directory, 'Logs', which is internally pointing to my shared drive folder which is access by a limited number of users. How can I create a log file on the virtual directory with log4net?

Do I (or can I) provide user credentials when I create the logger or the file?

1

There are 1 answers

0
Anthony Mastrean On

It looks like there's no problem writing log files to a virtual directory on your website. You just tell log4net that the path is relative to the current directory by writing the configuration

<appender ...>
    <file value="Logs\website.log" />
</appender>

Now, if you want to put the current username in the log message, you'll want to investigate log4net Contexts. Stashing the current user in the log4net context

log4net.ThreadContext.Properties["user"] = HttpContext.Cache["current-user"];

And pulling it out in the Appender layout

<appender ...>
    <layout ...>
        <conversionPattern value="%date %-5level [%property{user}] %message%newline" />
    </layout>
</appender>