Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C#: New log file daily?

Posted on 2004-10-03
5
Medium Priority
?
1,054 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 750 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 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