绕过 Windows 中的文件访问保护以只读方式查看活动日志文件
一位客户要求为 ASP.NET 应用程序提供快速而肮脏的日志查看器,我正在使用 log4net,我想我们可以简单地添加一个控制器来读取活动文件的尾部并将其吐回去。
如果我使用标准 .NET API(File.OpenText 等),我会遇到访问冲突(文件被另一个进程打开),这正是我所期望的,但我知道可以读取该文件,因为 Ultraedit 打开它进行查看只读。我可以通过 .NET API 执行同样的操作吗?
using(StreamReader infile =
System.IO.File.OpenText(Request.PhysicalApplicationPath + @"\log\my.log"))
{
}
A customer asked for quick and dirty log viewer for an ASP.NET app, for which I'm using log4net, and I thought somehow we could simply add a controller to read the tail of the active file and spit it back.
If I use the standard .NET API (File.OpenText, etc.) I get access violation (file open by another process), which is what I expect, but I know it is possible to read the file because Ultraedit opens it for viewing read-only. Can I do the same from the .NET API?
using(StreamReader infile =
System.IO.File.OpenText(Request.PhysicalApplicationPath + @"\log\my.log"))
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
指定您允许对文件进行读/写共享,并将 StreamReader 放在您的流上以获得与 File.OpenText 相同的行为。
由于您可以使用 UltraEdit 打开该文件,我假设 log4net 没有对该文件设置独占锁。
Specify That you allow read/write sharing on the file, and put a StreamReader on your stream to get the same behaviour as
File.OpenText
.And since you can open the file with UltraEdit, I assume log4net is not putting an exclusive lock on the file.
你尝试过吗:
Did you try: