简体   繁体   English

为什么我尝试使用C#写入日志文件时获得“不支持URI格式”异常?

[英]Why am I obtaining “URI formats are not supported” exception trying to write into a log file using C#?

I am writing a method that take a string containing a message and write this string into a log file. 我正在编写一个方法,该方法接受包含消息的字符串并将此字符串写入日志文件。

I have done in this way: 我这样做了:

internal static void WriteLogFile(string messageLog)
{
    if (messageLog == "")
    {
        messageLog = "L'import delle regole di inoltro è andato a buon fine. Tutte le regole di inoltro sono state inserite";
    }

    try
    {
        var filePath = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);

        Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");
        CultureInfo ci = new CultureInfo("it-IT");

        File.WriteAllText(filePath + "log.txt", messageLog);

        Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");

    }
    catch (Exception ex)
    {
        throw ex;
    }
}

The problem is that when perform this line: 问题在于执行此行时:

File.WriteAllText(filePath + "log.txt", messageLog);

I am obtaining the following exception: 我得到以下例外:

"URI formats are not supported."

What is wrong? 怎么了? What am I missing? 我错过了什么? How can I try to fix it? 我该如何解决这个问题?

Try this class: 试试这堂课:

using System;
using System.IO;

namespace YourNameSpace.Models
{
    public class Logger
    {
        private Object Locker { get; set; }
        public string Path { get; set; }

        public Logger(string path)
        {
            Locker = new Object();
            Path = path;
        }

        public void Log(string message, params object[] args)
        {
            lock (Locker)
            {
                string messageToLog = string.Format("{0} - {1}", DateTime.Now, string.Format(message, args));
                string path = System.IO.Path.Combine(Path, string.Format("{0}.txt", DateTime.Today.ToString("yyyyMMdd")));
                Directory.CreateDirectory(Path);
                File.AppendAllLines(path, new string[] { messageToLog });
            }
        }
    }
}

I'm assuming your problem is to write a log file in the same folder as your executable. 我假设您的问题是在与可执行文件相同的文件夹中写入日志文件。 Try to use the Location property: 尝试使用Location属性:

var filePath = Assembly.GetEntryAssembly().Location;

That will return a valid path that you can concatenate with the file name, or use Path.Combine method. 这将返回一个可以与文件名连接的有效路径,或使用Path.Combine方法。

Because WriteAllText does not support a URI format, and you're using a URI. 因为WriteAllText不支持URI格式,并且您正在使用URI。

Per https://docs.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=netframework-4.8 , you need to pass it a string path. 根据https://docs.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=netframework-4.8 ,您需要传递一个字符串路径。

As others have suggested, you should use GetPath if you want to create the file locally, or some other method depending where you want the file to go. 正如其他人所建议的那样,如果要在本地创建文件,则应使用GetPath,或者根据您希望文件的位置创建其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM