How To Create Error Log File In C#.net
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. Application Essentials File and Stream I/O Common I/O Tasks Common I/O Tasks How to: Open and Append to a Log File How to: Open and Append to a Log File How to: Open and Append to a Log File How to: Copy Directories How to: Enumerate Directories and Files How to: Read and Write to a Newly Created Data File How to: Open and Append to a Log File How to: Write Text to a File How to: Read Text from a File How to: Read Characters from a String How to: Write Characters to a String How to: Add or Remove Access Control List Entries How to: Compress and Extract Files Composing Streams How to: Convert Between .NET Framework Streams and Windows Runtime Streams TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. How to: Open and Append to a Log File .NET Framework (current version) Other Versions Visual Studio 2010 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 .NET Framework 2.0 .NET Framework 1.1 StreamWriter and StreamReader write characters to and read characters from streams. The following code example opens the log.txt file for input, or creates the file if it does not already exist, and appends information to the end of the file. The contents of the file are then written to standard output for display. As an alternative to this example, the information could be stored as a single string or string array, and the WriteAllText or WriteAllLines method could be used to achieve the same fun
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up how to save exception in txt file? up vote https://msdn.microsoft.com/en-us/library/3zc0w663(v=vs.110).aspx 5 down vote favorite 1 public DataTable InsertItemDetails(FeedRetailPL objFeedRetPL) { DataTable GetListID = new DataTable(); try { SqlParameter[] arParams = new SqlParameter[4]; arParams[0] = new SqlParameter("@Date", typeof(DateTime)); arParams[0].Value = objFeedRetPL.requestdate; } catch (Exception ex) { string dir = @"C:\Error.txt"; // folder location if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); File.AppendAllText(Server.MapPath("~/Error.txt"), "Message :" + ex.Message + "
" + Environment.NewLine + "StackTrace :" + ex.StackTrace + "" + Environment.NewLine + "Date :" + http://stackoverflow.com/questions/21307789/how-to-save-exception-in-txt-file DateTime.Now.ToString()); string New = Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine; File.AppendAllText(Server.MapPath("~/Error.txt"), New); } } } Here, I want to save an Exception in "C:\" ..I am trying In DAL... How to save the Exception In C drive Error.txt c# asp.net c#-4.0 exception try-catch share|improve this question edited Feb 14 '14 at 13:33 ekad 9,745112736 asked Jan 23 '14 at 12:11 Sambasiva 4721723 What error do you get from this code? –Steve Jan 23 '14 at 12:13 Use Log4Net for logging. –hkutluay Jan 23 '14 at 12:14 How to save the exception in Txt file...what i do? –Sambasiva Jan 23 '14 at 12:17 1 You don't need to check first if the Directory.Exists, you can use Directory.CreateDirectory(dir); directly since it will only create it if it does not already exist. –Tim Schmelter Jan 23 '14 at 12:26 add a comment| 1 Answer 1 active oldest votes up vote 28 down vote accepted Since you want to save the exception to C:\Error.txt, you don't need Directory.Exists, Directory.CreateDirectory, or Server.MapPath("~/Error.txt"). You can simply use StreamWriter like this: string filePath = @"C:\Error.txt"; using (StreamWriter writer = new StreamWriter(filePath, true)) { writer.WriteLine("Message :" + ex.Message + "
" + Environment.NewLine + "StackTrace
Party Controls ASP.Net Validators WCF Repeater Regular Expressions Yahoo API iTextSharp FaceBook Charts ListView Tweeter Google CSS SMS DotNetZip Crystal Reports Entity Framework HyperLink RDLC Report SqlDataSource Menu YouTube Twitter HTML XmlDataSource ListBox Tips http://www.aspsnippets.com/Articles/Create-simple-Error-Log-Text-File-in-ASPNet-using-C-and-VBNet.aspx DataGridView Cryptography Windows Forms LinkedIn WebUserControl RSS Feeds HTML5 Sitemap IIS LINQ DataPager URL Routing SqlBulkCopy OCR ASP.Net 4.5 Master Pages MySQL CSV Stored Procedures JSON Web Services Bootstrap Windows Service DataTable App.Config Visual Studio RadioButton CheckBox Generic Handler DropDownList FileUpload RadioButtonList CheckBoxList Flash HtmlEditorExtender Div Table AngularJS DataReader DataSet Console Applications FTP DetailsView Password TextBox Enum ComboBox Sponsored SqlDataAdapter OpenXml ClosedXml SiteMapPath Arrays FormView SqlTransaction Web.Config how to Cookies MVC Data Annotation Forums Contact Search Search Home Categories Forums Contact Search Create simple Error Log Text File in ASP.Net using C# and VB.Net 28 Oct 2015 Mudassar Khan 0 Comments 24058Views ASP.NetIssues and Exceptions Follow @ASPSnippets Follow @ASPSnippets Here Mudassar Ahmed Khan has explained with an example, how to create a simple Error Log Text File in ASP.Net using C# and VB.Net.Writing or Logging errors how to create (exceptions) occurring in ASP.Net application to Text files makes it easier to find the cause of the error as the Error Log Text File can be easily opened using the basic Notepad application in Windows. Download Download FREE APIs to work with ALL kinds of office files - http://e-iceblue.com/free-api Follow @ASPSnippets Follow @ASPSnippets In this article I will explain with an example, how to create a simple Error Log Text File in ASP.Net using C# and VB.Net. Writing or Logging errors (exceptions) occurring in ASP.Net application to Text files makes it easier to find the cause of the error as the Error Log Text File can be easily opened using the basic Notepad application in Windows. HTML Markup The following HTML Markup consists of an ASP.Net Button control which will raise an Exception and the exception (error) Message details will be displayed.