0

I am using an error log function which writes exceptions in a ~/App_Data/ErrorLog.txt file in the following format-

Created on: .... + Environment.NewLine
Error Description: .... + Environment.NewLine
Source File: .... + Environment.NewLine
Method: .... + Environment.NewLine
Line: .... + Environment.NewLine
Column: .... + Environment.NewLine
________________________________________________________________________

Now i am viewing it in browser. But NewLine character disappeared. I used System.IO.StreamReader to read the texts.

How can i keep the html as the above format?

Or, Is there any way to open the .txt file directly in browser from App_Data directory?

Thank you.

asked Dec 21, 2014 at 5:19

1 Answer 1

2

when you want to put it on the page replace \n with <br />:

StringBuilder sb = new StringBuilder();
sb.Append("This is a test" + "\n");
sb.Append("This is a test" + "\n");
sb.Append("This is a test" + "\n");
var result = sb.ToString().Replace("\n", "<br />");
// put result on the page
// note: StringBuilder works like StreamReader when you call StreamReader.ReadToEnd().Replace("\n", "<br />");
answered Dec 21, 2014 at 5:23
2
  • Thanks Mohamad. It works. But @Html.Raw(ViewBag.ErrorLogs) is needed to render it on razor view page. Anyway, +1. Commented Dec 21, 2014 at 5:36
  • Far better to just use css (whitespace: pre;) Commented Dec 21, 2014 at 5:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.