在C#中使用TraceSource方法无法写入日志文件 [英] can't write into log file using TraceSource Method in C#

查看:161
本文介绍了在C#中使用TraceSource方法无法写入日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的代码来记录异常。但它不是写任何登录到文件mylistener.log。我在这里缺少什么...

 使用系统;使用System.Diagnostics程序
;
命名空间TraceSourceApp
{
类节目
{
私有静态TraceSource MYSOURCE =新TraceSource(TraceSourceApp);
静态无效的主要(字串[] args)
{
TextWriterTraceListener会的TextListener =新TextWriterTraceListener会(myListener.log);

mySource.Listeners.Add(的TextListener);
INT I = 10,J = 0,K;

{
K = 1 / J;
}

{
mySource.TraceEvent(TraceEventType.Error,12,
除数为零);
}
mySource.Close();
}
}
}


解决方案

为了为TraceSource将数据写入文件,你需要设置的切换的TraceSource的-property到的 SourceSwitch 实例。改变你的代码如下:

 静态无效的主要(字串[] args)
{
TextWriterTraceListener会的TextListener =新TextWriterTraceListener会(myListener.log);
//新的代码从这里开始
变种sourceSwitch =新SourceSwitch(SourceSwitch,详细);
mySource.Switch = sourceSwitch;
//新的代码到此为止
mySource.Listeners.Add(的TextListener);
// ...

在除了具备自动TraceWriter刷新其内容,设置 Trace.AutoFlush 你的主要方法开始(采样工作没有它,但它始终是一个好主意,以确保听众是为了不丢失日志条目刷新):

 静态无效的主要(字串[] args)
{
Trace.AutoFlush = TRUE;
// ...



作为替代方案,你也可以明确地刷新监听调用其冲洗 -method底:

  textListener.Flush(); 
mySource.Close();

在现实世界中的代码,我建议加一个try-finally或使用块断言同花顺被调用,源被关闭。


i used the following code to log the exception. but its not write any logs into the file "mylistener.log". What am i missing here...

 using System;
    using System.Diagnostics;    
namespace TraceSourceApp
{
    class Program
    {
        private static TraceSource mySource = new TraceSource("TraceSourceApp");
        static void Main(string[] args)
        {          
            TextWriterTraceListener textListener = new TextWriterTraceListener("myListener.log");

            mySource.Listeners.Add(textListener);
            int i = 10, j = 0, k;
            try
            {
                k = i / j;
            }
            catch
            {
                mySource.TraceEvent(TraceEventType.Error, 12,
                    "Division by Zero");
            }      
            mySource.Close();               
        }           
    }
}

解决方案

In order for the TraceSource to write data to the file, you need to set the Switch-property of the TraceSource to a SourceSwitch instance. Change your code as follows:

static void Main(string[] args)
{
    TextWriterTraceListener textListener = new TextWriterTraceListener("myListener.log");
    // New code starts here
    var sourceSwitch =  new SourceSwitch("SourceSwitch", "Verbose");
    mySource.Switch = sourceSwitch;
    // New code ends here
    mySource.Listeners.Add(textListener);
    // ...

In addition to have the TraceWriter flush its content automatically, set Trace.AutoFlush at the beginning of you main method (the sample works without it, but it is always a good idea to make sure that the listeners are flushed in order not to loose log entries):

static void Main(string[] args)
{
    Trace.AutoFlush = true;
    // ...

As an alternative, you can also flush the listener explicitly by calling its Flush-method at the end:

textListener.Flush();
mySource.Close();

In real world code, I'd suggest to add a try-finally or using block to assert that Flush is called and that the source is closed.

这篇关于在C#中使用TraceSource方法无法写入日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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