StreamWriter用于编写异常的问题 [英] Problem with the StreamWriter for writing Exception

查看:91
本文介绍了StreamWriter用于编写异常的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我已经使用asp.net4创建了一个应用程序,对于这个问题,我想创建一个文本文件,任何时候任何异常引发,要在文本文件中写入异常消息。

所以我做了类似下面的事情,但它不起作用,事实上也没有写任何东西。



  public   class  EventLogger 
{
public static void 日志( string 消息,LogType类型)
{
string FullFileName = string .Format( {0} - {1} - {2} .log,DateTime。 Now.Year.ToString(),DateTime.Now.Month.ToString(),DateTime.Now.Day.ToString());
string FullPath = string .Format( {0} \\ {1},HttpContext.Current.Server.MapPath( / Log / Log.txt),FullFileName);
StreamWriter Writer = new StreamWriter(FullFileName, true ,System.Text.Encoding.UTF8) ;
Writer.WriteLine( string .Format( {0}#{1},Message.Replace( \\\\ n < br />),Type.ToString())) ;
Writer.Close();
Writer.Dispose();
}
}

public enum LogType
{
错误= -1,
警告= 0
正常= 1
}





我应该在解决方案中创建任何文本文件吗?

i将非常感谢帮助我找到问题。

解决方案

请参阅我对该问题的评论。我不相信你的记录器。为什么不使用知名的,开源的和经过充分测试的东西?考虑Apache log4net: http://logging.apache.org/log4net/ [ ^ ]。



-SA

Hello
I have made an application with asp.net4 and for this issue i want to make a text file that anytime any Exception raised , the Exception message to be written inside the text file.
so i have done something like below, but it doesn't work and infact does not write any thing.

public class EventLogger
        {
            public static void Log(string Message, LogType Type)
            {
                string FullFileName = string.Format("{0}-{1}-{2}.log", DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString());
                string FullPath = string.Format("{0}\\{1}", HttpContext.Current.Server.MapPath("/Log/Log.txt"), FullFileName);
                StreamWriter Writer = new StreamWriter(FullFileName, true, System.Text.Encoding.UTF8);
                Writer.WriteLine(string.Format("{0} # {1}", Message.Replace("\r\n", "<br />"), Type.ToString()));
                Writer.Close();
                Writer.Dispose();
            }
        }

        public enum LogType
        {
            Error = -1,
            Warning = 0,
            Normal = 1
        }



shall i create any text file in the solution ?
i will be thankfull to help me find the problem .

解决方案

Please see my comment to the question. I would not trust your logger. Why not using something well-known, open-source and well-tested? Consider Apache log4net: http://logging.apache.org/log4net/[^].

—SA


这篇关于StreamWriter用于编写异常的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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