Click here to Skip to main content
15,905,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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 .
Posted
Comments
Sergey Alexandrovich Kryukov 19-Aug-13 3:03am    
Ha, how do you know that another exception is not throwing as a result of logging?
—SA
mohammad ehsan 19-Aug-13 3:16am    
I have placed it for insert statement, and also i haven't made logging page , because it is a survey and does not need logging .
Sergey Alexandrovich Kryukov 19-Aug-13 3:26am    
"Does not need logging" has nothing to do with your question. You have some case where you need logging and the problem when you try to log. Please see my answer with some useful advice.
—SA
mohammad ehsan 19-Aug-13 3:36am    
so, does the above code is right or need to be corrected?
Sergey Alexandrovich Kryukov 19-Aug-13 9:16am    
I did not test it. It is itself can be a source of errors even if it works correctly in some cases.
I suggest to use a different logger. By the way, how about System.Diagniostics.EventLog?
—SA

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900