C# SteamWriter不想创建文件
我正试图将日志写入一个文件,这样每当我的应用程序崩溃时,日志仍然保存在日志文件中。但是StreamWriter不允许我,我已经尝试了所有可能的路径,但是每次我尝试测试它时,它都会抛出一个异常:C# SteamWriter不想创建文件,c#,C#,我正试图将日志写入一个文件,这样每当我的应用程序崩溃时,日志仍然保存在日志文件中。但是StreamWriter不允许我,我已经尝试了所有可能的路径,但是每次我尝试测试它时,它都会抛出一个异常: System.NotSupportedException: The given path's format is not supported. 但是,路径应该很好,如控制台输出所示: StartLogging=True Trying to write to C:\Users\Tom\documents\
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
任何帮助都将不胜感激,因为我不明白为什么这不起作用
保存日志功能:
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);
}
}
我认为你不能在文件名中使用冒号 从 文件名不能包含以下任何字符:
\ / : * ? " < > |
我认为你不能在文件名中使用冒号 从 文件名不能包含以下任何字符:
\ / : * ? " < > |
windows中的路径不能包含冒号:字符。路径可以。目录和文件名不能。windows中的路径不能包含冒号:字符。路径可以。目录名和文件名不正确。它是用来分隔驱动器和路径的。是的,你完全正确!我怎么没有看到这仍然是个谜,但我很高兴它被解决了。谢谢你的快速回答!对的它是用来分隔驱动器和路径的。是的,你完全正确!我怎么没有看到这仍然是个谜,但我很高兴它被解决了。谢谢你的快速回答!