我有一个日志文件旋转功能,看起来像这样:日志文件循环IOException异常
private static void RotateLogs()
{
FileInfo LogFile = new FileInfo(@"C:\temp\dataTransferErrorLog.txt");
if (LogFile.Exists && (LogFile.Length) >= 10 * 1048576)
{
Compress(LogFile);
LogFile.Delete();
File.Create(@"C:\temp\dataTransferErrorLog.txt");
}
}
private static void Compress(FileInfo fi)
{
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead())
{
// Prevent compressing hidden and
// already compressed files.
if ((File.GetAttributes(fi.FullName)
& FileAttributes.Hidden)
!= FileAttributes.Hidden & fi.Extension != ".gz")
{
string destFileName = fi.FullName.Substring(0, fi.FullName.LastIndexOf('.')) + System.DateTime.Now.ToString("yyyyMMddHHmm") + fi.FullName.Substring(fi.FullName.LastIndexOf('.')) + ".gz";
// Create the compressed file.
using (FileStream outFile =
File.Create(destFileName))
{
using (GZipStream Compress =
new GZipStream(outFile,
CompressionMode.Compress))
{
// Copy the source file into
// the compression stream.
inFile.CopyTo(Compress);
}
}
}
}
}
此抛出IOException它试图旋转日志文件的任何时间。我认为它试图写入.gz文件时引发异常,但我不确定。这是堆栈跟踪:
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException
Stack:
at System.IO.__Error.WinIOError(Int32, System.String)
at System.IO.FileStream.Init(System.String, System.IO.FileMode, System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32, System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean, Boolean)
at System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions)
at System.IO.StreamWriter.CreateFile(System.String, Boolean)
at System.IO.StreamWriter..ctor(System.String, Boolean, System.Text.Encoding, Int32)
at System.IO.StreamWriter..ctor(System.String, Boolean)
at System.IO.File.AppendText(System.String)
at XRayDataTransferService.XRayDataTransferService.LogMessage(System.String)
at XRayDataTransferService.XRayDataTransferService.RunAgent()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
有人可以证实,当它试图写入压缩信息的。广州文件,它是抛出异常,并告诉我的情况是什么原因造成它抛出异常?
编辑
这是LogMessage函数。大多数情况下,这种情况都会起作用,唯一一次抛出异常的情况是日志旋转的时候。
static void LogMessage(string messageText)
{
string ErrorLogFileName = @"C:\temp\dataTransferErrorLog.txt";
using (StreamWriter Log = File.AppendText(ErrorLogFileName))
{
try
{
Log.WriteLine("{0}: {1}", dateStamp, messageText);
}
catch { }
}
}
UPDATE:
static void RunAgent()
{
while (!shutdown)
{
LogMessage("Starting data transfer...");
errors = 0;
// Do some data processing
LogMessage("Finished running with " + errors.ToString() + " error(s).");
RotateLogs();
}
shutdownSignal.Set();
}
我在下面的评论说,但即使这样很明显,只有一个线程在运行。这是一项服务,因此必须位于单独的线程中,但只有一个线程正在运行。
你有没有尝试单步执行代码? – Derek
将您的pdb文件添加到代码运行的位置,您将在堆栈跟踪中获取行号。 – pstrjds
你正在使用什么日志库(如果有的话)?您是否可以开始使用NLog? –