我有一个看起来像这样的日志文件轮换函数:
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()
有人可以确认它在尝试将压缩信息写入 .gz 文件时抛出异常,并告诉我导致它抛出异常的情况如何?
编辑
这是 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 { }
}
}
更新:
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();
}
我已经在下面的评论中说明了,但很明显,只有一个线程在运行。这是一项服务,因此它必须位于单独的线程中,但只有一个线程在运行。