C#-记录器和服务的2个项目中的代码,错误



我正试图让我的EasyLogger C#项目正常工作,但我无法调试此代码,因为它是一个类库。我在Windows控制台应用程序中使用这个记录器,该应用程序作为Windows服务与松鼠一起安装,所以我看不到变量内部的内容。。。。

我做错了什么?

这是在Windows控制台应用程序中:

public void Start()
{
//Define EasyLogger
EasyLogger.Logger.Loggerpath = @"C:DevelopmentServerSF";
EasyLogger.Logger.Logname = "Testlog";
Thread.Sleep(1000);

//Start EasyLogger
bool test = EasyLogger.Logger.Log;
EasyLogger.Logger.StartLog();
if (test == true)
{
//Start service
_timer.Start();
ExThred(configManager.ServerName);
}
else
{
//ERROR log isnt started
}
}

这是我的EasyLogger项目:

using System;
using System.IO;
using System.Text;
namespace EasyLogger
{
public static class Logger
{
#region GET SET      
private static string loggerpath;
private static string logname;
private static string message;
private static bool log;
public static string Loggerpath
{
get => loggerpath;
set => loggerpath = value;
}
public static string Logname
{
get => logname;
set => logname = value;
}
public static string Message
{
get => message;
set => message = value;
}
public static bool Log
{
get => log;
set => log = value;
}
#endregion
#region Logger
private static readonly string loggingpath = loggerpath + @"logs"+ logname + "-" + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + ".txt";
public static void StartLog()
{
if(loggerpath == null)
{
Message = "Program path is empty!";
log = false;
}
else
{
try
{
using (FileStream fs = File.Create(loggingpath))
{
byte[] info = new UTF8Encoding(true).GetBytes(DateTime.Now.ToString() + " - Logger v0.1 Started...");
fs.Write(info, 0, info.Length);
}
log = true;
}
catch
{
Message = "Can't create logfile!";
log = false;
}  
}
}
public static void WriteLog(string msg)
{
if (Logname != null)
{
if (log == true)
{
string[] lines = new string[] { DateTime.Now.ToString() + " - " + msg };
File.AppendAllLines(loggingpath, lines);
}
else
{
try { Message = "Logger isn't started yet!"; }
catch { Message = "Logger Error!"; }
}
}
else { Message = "Please define first a Logname!"; }
}
public static void StopLog()
{
log = false;
}
#endregion
}
}

问题不在于记录器。您的问题是如何调试Windows服务

使用Visual Studio,您可以这样做:

  1. 使用调试信息编译Windows服务
  2. 安装服务并运行它
  3. 启动Visual Studio
  4. 选择";调试&quot--&gt"附加过程">
  5. 单击";显示来自所有用户的进程">
  6. 搜索您的服务并点击";附加">
  7. 选择";公共语言运行时">

最新更新