我正在尝试将我的日志写入文件,因此每当我的应用程序崩溃时,日志仍会保存在日志文件中。然而,StreamWriter不允许我,我已经尝试了每个路径的可能性,但每次我尝试测试它时它会抛出这个异常:
System.NotSupportedException: The given path's format is not supported.
然而路径应该没问题,如控制台输出中所示:
StartLogging=True Trying to write to C:\Users\Tom\documents\visual studio 2012\Projects\xxx\yyy\bin\Debug\logs\06-11-13_11:51:37.log
任何帮助都会受到高度赞赏,因为我无法理解为什么这不起作用......
SaveLog功能:
public void SaveLog(bool startLogging = false)
{
DateTime startTime = LogManager.StartTime;
string appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string logPath = appPath.Replace("file:\\", "") + "\\logs\\";
string logFile = startTime.ToString("MM-dd-yy_HH:mm:ss") + ".log";
string fullPath = Path.Combine(logPath, logFile);
if (!Directory.Exists(logPath))
{
Directory.CreateDirectory(logPath);
}
Console.WriteLine("StartLogging={0} Trying to write to {1}", startLogging, fullPath);
using (StreamWriter writer = new StreamWriter(fullPath, !startLogging))
{
writer.Write("[{0}][{1}] ", Time.ToString("HH:mm:ss:fff"), Level.ToString());
writer.Write(Message);
writer.WriteLine(" ({0} - {1})", Method, Location);
}
}
答案 0 :(得分:8)