Solved

C#: New log file daily?

Posted on 2004-10-03
5
1,004 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

22 Experts available now in Live!

Get 1:1 Help Now