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
}
}
}
}