C#:无法写入已关闭的TextWriter [英] C#: Cannot write to a closed TextWriter

查看:1165
本文介绍了C#:无法写入已关闭的TextWriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有



我必须在我的程序中创建错误日志。



i能够写出第一行但后来却抛出错误无法写一个封闭的文本作者



这是我的代码



System.IO.StreamWriter elog = new System.IO.StreamWriter(@〜\ Logs \insert_into_table+ DateTime.Now。 ToString(ddMMyyyy)+。+ DateTime.Now.ToString(hhmmss)+。log,true);



使用(elog)

{



elog.WriteLine(/ ******************* *****************日志已开始******************************* ************************ /);

} ////此行写得很好





试试

{



使用(elog)

{

elog.WriteLine(连接到数据库打开); /// 在这里抛出异常

elog.WriteLine();

}



/









}



catch(例外ee)

{

使用(elog)

{

elog.WriteLine(例外情况发生。+ ee.Message); /// 在这里抛出异常



elog.WriteLine();

}

Console.Write(ee);

}





}

}

}

Dear all

I have to create error logs in my program.

i am able to write first line but afterwards it throws error " cannot write a to closed textwriter"

here is my code

System.IO.StreamWriter elog = new System.IO.StreamWriter(@"~\Logs\insert_into_table" + DateTime.Now.ToString("ddMMyyyy") + "." + DateTime.Now.ToString("hhmmss") + ".log", true);

using (elog)
{

elog.WriteLine("/************************************Logs Started*******************************************************/");
} ////this line writes well


try
{

using (elog)
{
elog.WriteLine("Connection to database open"); /// throwing exception here
elog.WriteLine("");
}

/




}

catch (Exception ee)
{
using (elog)
{
elog.WriteLine("exception occured. " + ee.Message); /// throwing exception here too

elog.WriteLine("");
}
Console.Write(ee);
}


}
}
}

推荐答案

只需删除using()块,它会自动释放对象。
Just remove using() block, it automatically release the object at end.


这篇关于C#:无法写入已关闭的TextWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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