从 .NET 应用程序读取 Confluence 日志文件
我在 Windows 2008 R2 机器上设置了一个 Confluence 3.5 站点。它使用 Tomcat 6,我已将其配置为使用 log4j 对特定空间进行访问日志记录。日志记录工作正常,但现在我想在.NET 中编写一个 Windows 服务来每天对日志数据进行一些特定的解析。在我的服务中,我使用 File.ReadAllText 读取整个文件(文件本身不大,也不是 XML)。但是当它尝试这样做时,我收到一个 IO 异常,表明该文件正在被另一个进程使用。
我已经尝试了各种方法来解决这个问题,但到目前为止我似乎无法从代码中读取日志文件。我可以登录服务器并使用 Windows 资源管理器制作它的副本。但如果我尝试在服务中执行此操作,则会收到相同的错误。我在网上寻找读取日志文件的方法,或者更改记录器以不独占打开文件,但我没有找到任何内容。我不确定首先打开该文件的是 Confluence、Tomcat 还是 log4j。
关于如何使用自定义程序读取访问日志有什么建议吗?
I have a Confluence 3.5 site set up on a Windows 2008 R2 box. It uses Tomcat 6 and I've configured it to do access logging of a particular space using log4j. The logging works fine, but now I want to write a Windows service in .NET to do some specific parsing of the log data on a daily basis. In my service I read the entire file using File.ReadAllText (the file itself is not large, nor is it XML). But when it tries to do that, I get an IO exception that says the file is in use by another process.
I've tried various things to get around this, but so far I can't seem to read the log file from code. I can log into the server and make a copy of it using Windows Explorer. But if I try to do that in the service, I get the same error. I've looked around the web for ways of reading the log file, or maybe changing the logger to not have the file open exclusively, but I haven't found anything. I'm not sure if it's Confluence, Tomcat, or log4j that opens the file in the first place.
Any suggestions on how to read from the access log using a custom program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
出于性能原因,记录器可能会锁定文件(例如,这样它就不必不断打开文件)。要确认这一点,请登录服务器并使用 Unlocker 和 Unlocker 和 < a href="http://technet.microsoft.com/en-us/sysinternals/bb896653" rel="nofollow">进程资源管理器。
您应该能够配置记录器,使其不持有锁定。我之前没有使用过 log4j,但是请查看手册/网站 - 您可以做一些事情。
The logger is probably holding a lock on the file for performance reasons (e.g. so it doesn't have to constantly open the file). To confirm this, log into the server and check for any open handles on the file using either Unlocker and Process Explorer.
You should be able to configure the logger so that it does not hold the lock. I haven't used log4j before however check the manual/website - there will be something you can do.
您可以打开 FileStream(String, FileMode, FileAccess 、FileShare) 在 C# 中使用 FileShare.ReadWrite。
这应该可以防止您的文件流尝试锁定该文件,并且不应引发任何异常。
如果您仅使用不带 FileShare 枚举参数的 CTOR,则流默认共享文件仅用于读取:
You could open a FileStream(String, FileMode, FileAccess, FileShare) in C# with FileShare.ReadWrite.
That should prevent that your filestream trys to get a lock on that file and no exception should be thrown.
If you just use the CTOR without the FileShare Enum Parameter the stream shares the file per default only for reading: