1

我正在尝试将我的日志写入一个文件,这样每当我的应用程序崩溃时,日志仍然保存在日志文件中。但是 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

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

保存日志功能:

    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);
        }
    }
4

1 回答 1

8

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

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

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

\ / : * ? " < > |
于 2013-06-11T10:03:28.677 回答