如何使 main 作为客户端 dll 和记录器类应作为服务运行
使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用系统文本;
命名空间堵塞器 { 班级计划 { 静态无效主(字符串[]参数) { CLogger.Trace("我的班级"); CLogger.Error(“消息”); CLogger.Warning(“战争”);
}
}
使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用系统文本; 使用系统.IO;
命名空间堵塞器 { CLogger类 { 静态 FileStream fs = new FileStream(@"c:\vijay\mcb.txt", FileMode.OpenOrCreate, FileAccess.Write); 静态 StreamWriter m_streamwriter = new StreamWriter(fs);
public static void Trace(string p)
{
m_streamwriter.WriteLine("Trace(string p)");
}
public static void Error(string p)
{
m_streamwriter.WriteLine("Error(string p)");
m_streamwriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamwriter.WriteLine("File writer operation starts: ");
m_streamwriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
}
public static void Warning(string p)
{
m_streamwriter.WriteLine("Warning(string p)");
m_streamwriter.Flush();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace clogger
{
class Program
{
static void Main(string[] args)
{
CLogger.Trace("my class");
CLogger.Error("message");
CLogger.Warning("war");
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace clogger
{
class CLogger
{
static FileStream fs = new FileStream(@"c:\vijay\mcb.txt", FileMode.OpenOrCreate, FileAccess.Write);
static StreamWriter m_streamwriter = new StreamWriter(fs);
public static void Trace(string p)
{
m_streamwriter.WriteLine("Trace(string p)");
}
public static void Error(string p)
{
m_streamwriter.WriteLine("Error(string p)");
m_streamwriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamwriter.WriteLine("File writer operation starts: ");
m_streamwriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
}
public static void Warning(string p)
{
m_streamwriter.WriteLine("Warning(string p)");
m_streamwriter.Flush();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在研究使用 .Net 进行日志记录,请查看 Log4Net (以及自定义 TraceListener,如果您不想让您的代码依赖于第 3 方软件)。 Log4Net 是一个开源项目,在日志记录方面被广泛使用。我发现这个教程非常有帮助官方网站也会为你提供很多信息,我发现这样更容易上手。顺便说一下,我认为 Log4Net 的一大优点是,如果您决定要记录到 xml 文件,只需对 app.config 进行简单的更改,即无需编写额外的代码。
为什么要将记录器作为服务运行?如果您只写入本地 Txt 文件,l4n 将完成这项工作,否则您仍然可以编写自己的远程附加程序,然后编写一个套接字侦听器或 wcf udp 侦听器,将您的数据写入持久位置。
If you are looking into logging with .Net, check out Log4Net (and custom TraceListener if you don't want to make your code dependent on 3rd party software). Log4Net is an opensource project and is used a lot when it comes to logging. I found this tutorial quite help full though the official site will also provide you with a lot of information, I found this easier to get started. By the way a great plus of Log4Net in my opinion is that if you ever decide you want to log to an xml file it's a simple change in your app.config i.e. no writing of additional code.
Why do you want to run your logger as a service? If you only write to a local Txt file l4n will do the job and otherwise you can still write your own remote appender and then write a socketlistener or wcf udp listener that will write your data to a persistent place.