0

I am saving log messages in my logtext.txt file.I want to give numbering for log messages in this file, Is there any solution to acieve this? Below is my code:

// Create a writer and open the file:
StreamWriter log;

if (!File.Exists(AssetXMLLogMessagesPath + ".txt"))
{
    log = new StreamWriter( AssetXMLLogMessagesPath + ".txt", true);
}
else
{
    log = File.AppendText(AssetXMLLogMessagesPath + ".txt");
}                

// Write to the file:
log.WriteLine(" "+"<------------------------------"+" AssetImporter at "+":" +" "+ DateTime.Now.ToString("F") + "--------------------------------------->");
log.WriteLine(msg);                
log.WriteLine();

// Close the stream:
log.Close();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Indra
  • 279
  • 2
  • 3
  • 16
  • How do you log? If you have a class that manages the logging it could hold a property `int LogMessageCount` which increases on every `log.WriteLine` (just pseudo-code). – Tim Schmelter Sep 13 '13 at 15:17
  • I want to add like 1. Message1, 2.Message2... – Indra Sep 13 '13 at 15:20
  • Yes, but what have you tried, where did you get stuck? What was the problem? How are you logging at all? Show us some code please. – Tim Schmelter Sep 13 '13 at 15:22

2 Answers2

0

You may try like this:-

public sealed class LineCounter : PatternLayoutConverter
{       
    private static int i= 0;

    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        LineCounter.i++;
        writer.Write(LineCounter.i.ToString());
    }
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You have to retain the message-number yourself. So i would create a class that is responsible for the logging functionality. There you add a property LogEntries which gets increased on every new log-message.

For example:

public static class VerySimpleLogger
{
    public static string Path{ get; set; }
    public static int LogEntries { get; set; }
    public static bool WithTimeStamp { get; set; }

    public static void Log(string message)
    {
        LogEntries++;
        if(WithTimeStamp)
            message = string.Format("{0}. {1}:\t{2}{3}", LogEntries, DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(), message, Environment.NewLine);
        else
            message = string.Format("{0}.\t{1}{2}", LogEntries, message, Environment.NewLine);
        File.AppendAllText(Path, message);
    }
}

Usage:

VerySimpleLogger.Path = @"C:\Temp\logtext.txt";
for (int i = 1; i <= 100; i++)
{
    VerySimpleLogger.Log("Log-message #" + i);
}

But note that the LogEntries number will be zero again if you restart the program. It will not count the lines in a file. So this might be perfect for tools where you need to create a log-file for one execution or for long running applications like windows-services. If this is a winforms application that is used from multiple users and all should share the same log file, it is not a viable approach.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939