如何在不使用第三方库的情况下登录C#? [英] How do I do logging in C# without using 3rd party libraries?

查看:70
本文介绍了如何在不使用第三方库的情况下登录C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中实现日志记录,但是宁愿不使用任何外部框架,例如log4net.

I would like to implement logging in my application, but would rather not use any outside frameworks like log4net.

因此,我想对文件执行DOS的 echo 之类的操作.最有效的方法是什么?

So I would like to do something like DOS's echo to a file. What is the most effective way to do it?

是否有一种方法可以在不使用外部框架的情况下记录未处理的异常?

Is there a way to log unhandled exceptions logged without using an outside framework?

推荐答案

public void Logger(string lines)
{
  //Write the string to a file.append mode is enabled so that the log
  //lines get appended to  test.txt than wiping content and writing the log

  using(System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt", true))
  {
    file.WriteLine(lines);
  }
}

有关更多信息, MSDN

这篇关于如何在不使用第三方库的情况下登录C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆