Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Witing ASP.NET C# code to a log file.

Status
Not open for further replies.

xarzu

IS-IT--Management
Jan 16, 2012
17
0
1
US
I would like to have some way of tracing an ASP.NET application in production that is no longer in debug mode. I suppose I can try with some sort of pop-up windows, but, before I give that a try, I want to try to implement some way of the code writing out content on the server into a log file.

The code that I have inherited from a developer that is no longer with us, has an existing class that seems to be designed for this purpose:

Code:
public class DebugTextWriter : System.IO.TextWriter
    {
        public override void Write(char[] buffer, int index, int count)
        {
            HttpContext.Current.Response.Write("<textarea>" + new String(buffer, index, count) + "</textarea>");
           
        }

        public override void Write(string value)
        {
            HttpContext.Current.Response.Write("<textarea>" + value + "</textarea>");
           
        }

        public override Encoding Encoding
        {
            get { return System.Text.Encoding.Default; }
        }
    }

This does not seem to do much of anything to me. Where is the code written to?

When I look at the bit of code that implements this class, I do not see a file referenced:

Code:
Applications.DebugTextWriter dbgTextwriter = new DebugTextWriter();
            dbgTextwriter.Wriet("some message") ;

Any suggestions? Where would the output log file be if one is not specified?


 
First of all you should decide _what_ exactly you wish to log and _when_.
You surely do not want to end up with a 1GB log file after a week or so.

This for example would be a path to a subfolder "App_Data" in my web root:
Code:
using System.IO;

string logfile = Path.Combine(Server.MapPath("~/App_Data"), "log.txt");

You could then wrap critical code portions in a try/catch block and write errors to the log file like this:
Code:
try
            {
               ...
            }
            catch (Exception ex)
            {
                using (StreamWriter sw = new StreamWriter(logfile, true, Encoding.UTF8))
                {
                    sw.Write("Error in method yaddayadda, my while-loop: " + ex.Message + ", " + ex.Source);
                }
            }

The "true" parameter in the StreamWriter is for "append", meaning it will create the log file if it does not exist yet, else it will append to the existing log.

Hope this helps.

Chhers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
What do you mean by tracing? do you want to just log errors somewhere that you can monitor? Are you looking to monitor performance.

As for errors, Microsoft has Application Blocks, now called enterprise library that has different pieces to it. One of which is logging. "Exception Handling Application Block."

This is easily incorporated into your app or web and is easy to use. You can start here:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top