Solved

C#: New log file daily?

Posted on 2004-10-03
5
1,041 Views
Last Modified: 2012-08-13
Q. How can I create a new log file each day using C#?
0
Comment
Question by:kvnsdr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

691 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