开发者

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

开发者 https://www.devze.com 2023-02-09 23:14 出处:网络
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 whic开发者_开发百科h is access by a limited numbe

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 whic开发者_开发百科h 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?


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消