四处逛逛文件访问保护在Windows中查看活跃日志文件只读 [英] Getting around file access protection in Windows to view an active logfile read-only

查看:125
本文介绍了四处逛逛文件访问保护在Windows中查看活跃日志文件只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个客户要求快速和肮脏的日志查看器为ASP.NET应用程序,为此,我使用log4net的,而且我认为在某种程度上,我们可以简单地增加一个控制器读取活动文件的尾部,吐了回去。

如果我用的是标准的.NET API(File.OpenText等),我得到的访问冲突(文件打开由另一个进程),这是我所期待的,但我知道这是可以读取该文件,因为打开用Ultraedit它用于查看只读。我可以做同样的.NET API?

 使用(StreamReader的INFILE =
         System.IO.File.OpenText(Request.PhysicalApplicationPath + @\登录\ my.log))
{
}
 

解决方案

指定您允许读/对文件写入共享,并把一个StreamReader您的流,以获得相同的行为文件。 OpenText公司

 使用(流流= File.open方法(@X:\路径\ file.log
        FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
{
    使用(StreamReader的SR =新的StreamReader(流))
    {
        //读取内容
    }
}
 

既然你可以打开用UltraEdit的文件,我想log4net的是不是在文件上放置一个独占锁。

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"))
{
}

解决方案

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.

using( Stream stream = File.Open(@"x:\path\file.log", 
        FileMode.Open, FileAccess.Read, FileShare.ReadWrite) )
{
    using(StreamReader sr = new StreamReader(stream))
    {
        //read content
    }
}

And since you can open the file with UltraEdit, I assume log4net is not putting an exclusive lock on the file.

这篇关于四处逛逛文件访问保护在Windows中查看活跃日志文件只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆