IAd*_*ter 90 .net c# performance logging winforms
我想在我的应用程序中实现日志记录,但宁愿不使用任何外部框架,如log4net.
所以我想做一些像DOS的文件回声.最有效的方法是什么?
有没有办法记录未使用外部框架记录的未处理的异常?
Bra*_*don 63
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
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt",true);
file.WriteLine(lines);
file.Close();
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息MSDN:
emp*_*mpi 22
我宁愿不使用像log4j.net这样的外部框架.
为什么?Log4net可能会满足您的大多数要求.例如,检查此类:RollingFileAppender.
Log4net已有详细记录,网上有数千个资源和用例.
šlj*_*ker 16
您可以直接写入事件日志.请查看以下链接:
http ://support.microsoft.com/kb/307024
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx
以下是来自MSDN的示例:
using System;
using System.Diagnostics;
using System.Threading;
class MySample{
public static void Main(){
// Create the source, if it does not already exist.
if(!EventLog.SourceExists("MySource"))
{
//An event log source should not be created and immediately used.
//There is a latency time to enable the source, it should be created
//prior to executing the application that uses the source.
//Execute this sample a second time to use the new source.
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatedEventSource");
Console.WriteLine("Exiting, execute the application a second time to use the source.");
// The source is created. Exit the application to allow it to be registered.
return;
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";
// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");
}
}
Run Code Online (Sandbox Code Playgroud)
liv*_*ove 15
如果您正在寻找一种真正简单的记录方式,您可以使用这一个衬垫.如果该文件不存在,则创建该文件.
System.IO.File.AppendAllText(@"c:\log.txt", "mymsg\n");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
204652 次 |
| 最近记录: |