SteamWriter不想创建文件

Kry*_*oxx 1 c#

我正在尝试将我的日志写入文件,因此每当我的应用程序崩溃时,日志仍保存在日志文件中.然而,StreamWriter不允许我,我已经尝试了每个路径的可能性,但每次我尝试测试它时它会抛出这个异常:

System.NotSupportedException: The given path's format is not supported.
Run Code Online (Sandbox Code Playgroud)

然而路径应该没问题,如控制台输出中所示:

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
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,因为我不明白为什么这不起作用...

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Skl*_*vvz 8

我认为您不能:在文件名中使用冒号().

来自http://support.microsoft.com/kb/177506

文件名不能包含以下任何字符:

\ / : * ? " < > |
Run Code Online (Sandbox Code Playgroud)