c# - 使用 Form 应用程序更改记录器的路径并使用 C# 将其实现为服务
问题描述
我正在开发一个 File Watcher 服务,其中也有一个表单应用程序(同一解决方案中有 2 个不同的项目)。因此,我得到了使用 Forms 应用程序保存日志的路径。然后我把它放在我的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>
我有一个Variables
定义变量的类。
using System.Configuration;
namespace FileWatchingService
{
public static class Variables
{
public static string FilePath { get; set; } = ConfigurationManager.AppSettings.Get("location");
public static string LogPath { get; set; } = ConfigurationManager.AppSettings.Get("logLocation");
}
}
然后我试着把我的 LogPath 放在这里:
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
}
}
}
}
问题是我的方法行不通。我可以做些什么来更改我的日志文件路径?
解决方案
仅查看代码,您似乎在LogPath值\
的末尾缺少 a 。您还可以执行或仅定义 LogPath 本身,例如:File.AppendAllText(Variables.LogPath + "\logFile.log", _message);
<appSettings>
<add key="location" value="C:\Users\user\Documents" />
<add key="logLocation" value="C:\Users\user\Documents\log.txt" /> <!-- the file itself -->
</appSettings>
推荐阅读
- java - 带有 java 包的 Heroku web python 应用程序:如何设置 Procfile
- javascript - 如何通过页面加载使切换处于打开状态,而其他切换保持关闭
- reactjs - 为所有组件/html标签创建和处理自定义DOM属性
- node.js - 以非阻塞方式在 http post 请求上写入 excel
- c# - 使用 C# 中的 API 在 ivanti HEAT 中创建 Note 对象
- microsoft-graph-api - 列出存档的团队
- python - 如何使用python从列值中删除特殊字符
- android - 在颤振中使用 orderBy() 时,firebase firestore 没有任何值
- c# - SignalR - 如何保护客户端以确保用户不会创建自己的客户端
- node.js - Fastify 如何计算响应时间?