Solved

C#: New log file daily?

Posted on 2004-10-03
5
1,016 Views
Last Modified: 2012-08-13
Q. How can I create a new log file each day using C#?
0
Comment
Question by:kvnsdr
  • 3
5 Comments
 
LVL 3

Expert Comment

by:a_goat
ID: 12212276
Use a logging package.  It makes your logging much more robust and you can do things like that easily.  The best two I've seen are log4net ( http://logging.apache.org/log4net/ ) and NSpring Logging ( http://sourceforge.net/projects/nspring )

Both offer capabilities like rolling files based on date and buffered output as well as multiple output streams.  I just finished a project that uses NSpring to simultaneous log to Console, Debug Console, Files, Event Logs and Telnet sessions based on various criteria of the message.
0
 
LVL 11

Expert Comment

by:sachiek
ID: 12213992
CreateLogFiles.cs

using System;
using System.IO;
using System.Text;

namespace CreateLogFiles
{
      public class CreateLogFiles
      {
            private string sLogFormat;
            private string sErrorTime;

            public CreateLogFiles()
            {
                  //sLogFormat used to create log files format :
                  // dd/mm/yyyy hh:mm:ss AM/PM ==> Log Message
                  sLogFormat = DateTime.Now.ToShortDateString().ToString()+" "+DateTime.Now.ToLongTimeString().ToString()+" ==> ";
                  
                  //this variable used to create log filename format "
                  //for example filename : ErrorLogYYYYMMDD
                  string sYear      = DateTime.Now.Year.ToString();
                  string sMonth      = DateTime.Now.Month.ToString();
                  string sDay      = DateTime.Now.Day.ToString();
                  sErrorTime = sYear+sMonth+sDay;
            }

            public void ErrorLog(string sPathName, string sErrMsg)
            {
                  StreamWriter sw = new StreamWriter(sPathName+sErrorTime,true);
                  sw.WriteLine(sLogFormat + sErrMsg);
                  sw.Flush();
                  sw.Close();
            }
      }
}


0
 
LVL 11

Accepted Solution

by:
sachiek earned 250 total points
ID: 12213995
CreateLogFiles.aspx

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

namespace CreateLogFiles
{
      /// <summary>
      /// Summary description for CreateLogFiles1.
      /// </summary>
      public class CreateLogFiles1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Label Label1;
            protected System.Web.UI.WebControls.TextBox TxtFilename;
            protected System.Web.UI.WebControls.Button BtnFind;
            protected System.Web.UI.WebControls.Label Msg;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.BtnFind.Click += new System.EventHandler(this.BtnFind_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void BtnFind_Click(object sender, System.EventArgs e)
            {
                  try
                  {
                        StreamReader sr = new StreamReader(this.TxtFilename.Text);
                        sr.Read();
                        sr.Close();
                        Msg.Visible = true;
                        Msg.Text = "File "+ this.TxtFilename.Text +" was found";
                  }
                  catch(Exception ex)
                  {
                        CreateLogFiles Err = new CreateLogFiles();
                        Err.ErrorLog(Server.MapPath("Logs/ErrorLog"),ex.Message);
                        Msg.Visible = true;
                        Msg.Text = "Fatal error : "+ ex.Message + ", please find a complete error at ErrorLog file";
                  }
            }
      }
}

0
 
LVL 11

Expert Comment

by:sachiek
ID: 12214001
Note: Create a folder called "Logs" to test this application in under the path which you are saving your project.

Hope you got it right.

Cheers!
Sachi
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now