[以C#写入日志文件时StreamWriter引发NotSupportedException

问题描述 投票:0回答:1

我正在尝试将异常写入.log文件,当它们发生时。我编写了一种方法来实现此目的,但是当我尝试使用StreamWriter写入文件时,总是收到消息

“ System.NotSupportedException:给定路径的格式不是受支持。“

我已经尝试了Internet上的几乎所有解决方案,但到目前为止,没有任何效果。我将不胜感激。谢谢。

public static void WriteToErrorLog(string error, string errorType)
        {
            try
            {
                string fileName = "log-" + errorType + "-" + (DateTime.Now).ToString("MM-dd-yyyy-HH:mm") + ".log";
                StreamWriter file = new StreamWriter(fileName);
                file.Write(error);
                file.Close(); ;
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
                Console.Write(e);
            }
        }
c# file-io streamwriter logfile notsupportedexception
1个回答
0
投票

您的文件名不正确

string fileName = "log-" + errorType + "-" + (DateTime.Now).ToString("MM-dd-yyyy-HH:mm") + ".log";

尝试切换到不同的DateTime格式,例如yyyy-dd-MM HH-mm-ss这样的格式>

© www.soinside.com 2019 - 2024. All rights reserved.