Solved

C#: New log file daily?

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

Suggested Solutions

Title # Comments Views Activity
Securing WEBAPI on Azure 2 54
Create a Dropdown list with sub group 10 39
C# DataTable (in memory) vs SQL Lookups 4 68
asp.net repeater 2 33
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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