I recently added log4net logging to this website. After configuring log4net and adding logging code the the web application, I immediately added a page to my administration site to display the log. Unfortunately, the initial code I wrote threw an exception because, not surprisingly, the log4net log file was locked.
The trick is to open the file in FileShare.ReadWrite mode:
...
protected string logFileText;
protected void Page_Load(object sender, EventArgs e)
{
string path = Server.MapPath("~/logs/log.txt");
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader streamReader = new StreamReader(fileStream))
{
logFileText = streamReader.ReadToEnd();
}
}
...
| Name | Comment | URL | Date/Time |
| Linus | The solution provided by Sunil is not the same and will not solve the same issues, as "MinimalLock" can still lock the file, as I already had that set and needed to stream. | December 19, 2014 | |
| Sunil Sharma | I found a better method to do this at: http://hoenes.blogspot.com/2006/08/displaying-log4net-log-file-on-aspnet.html | http://hoenes.blogspot.com/2006/08/displaying-log4net-log-file-on-aspnet.html | February 18, 2009 |
| mr mO | Thank you sooo very much! | http://mokhan.ca | July 24, 2008 |
| Title | Date |
| Node.js + Express: How to Block Requests by User-Agent Headers | January 7, 2026 |
| Vault 3 is Now Available for Windows on ARM Machines! | December 13, 2025 |
| Vault 3: How to Include Outline Text in Exported Photos | October 26, 2025 |
| .NET Public-Key (Asymmetric) Cryptography Demo | July 20, 2025 |
| Raspberry Pi 3B+ Photo Frame | June 17, 2025 |
| EBTCalc (Android) Version 1.53 is now available | May 19, 2024 |
| Vault 3 Security Enhancements | October 24, 2023 |