四处逛逛文件访问保护在Windows中查看活跃日志文件只读文件、活跃、日志、Windows

2023-09-02 02:05:31 作者:凉薄无爱

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

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

 使用(StreamReader的INFILE =
         System.IO.File.OpenText(Request.PhysicalApplicationPath + @登录 my.log))
{
}
 
windows 资源保护找到了损坏文件但无法修复其中某些文件

解决方案

指定您允许读/对文件写入共享,并把一个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 + @"logmy.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:pathfile.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.