Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用表单应用程序更改记录器的路径并将其实现为使用C服务#_C#_Winforms_Service - Fatal编程技术网

C# 使用表单应用程序更改记录器的路径并将其实现为使用C服务#

C# 使用表单应用程序更改记录器的路径并将其实现为使用C服务#,c#,winforms,service,C#,Winforms,Service,我正在从事一个文件观察服务,其中也有一个表单应用程序(在同一个解决方案中有两个不同的项目)。因此,我得到了一个路径,用于在表单应用程序中保存日志。然后我把它放在我的app.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="location" val

我正在从事一个文件观察服务,其中也有一个表单应用程序(在同一个解决方案中有两个不同的项目)。因此,我得到了一个路径,用于在表单应用程序中保存日志。然后我把它放在我的
app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="location" value="C:\Users\user\Documents" />
        <add key="logLocation" value="C:\Users\user\Documents" /> <!-- this is where it changes save it-->
    </appSettings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
</configuration>
然后我尝试将我的日志路径放在这里:

using System;
using System.IO;

namespace FileWatchingService
{
    public static class Logger
    {
        public static void Log(string message)
        {
            try
            {
                string _message = String.Format("{0} {1}", message, Environment.NewLine);
                //File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "logFile.log", _message);
                File.AppendAllText(Variables.LogPath + "logFile.log", _message);
            }
            catch (Exception ex)
            {
                //Implement logging on next version
            }
        }
    }
}

问题是我的方法行不通。如何更改日志文件路径?

仅查看代码,您似乎在日志路径值的末尾缺少一个
\
。 您还可以执行
File.AppendAllText(Variables.LogPath+“\logFile.log”,_message)或仅定义日志路径本身,例如:

using System;
using System.IO;

namespace FileWatchingService
{
    public static class Logger
    {
        public static void Log(string message)
        {
            try
            {
                string _message = String.Format("{0} {1}", message, Environment.NewLine);
                //File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "logFile.log", _message);
                File.AppendAllText(Variables.LogPath + "logFile.log", _message);
            }
            catch (Exception ex)
            {
                //Implement logging on next version
            }
        }
    }
}