A.Oreo
A.Oreo

Reputation: 321

C# Logger.Write clear the previous records

I want to use

Logger.Write(sw.ElapsedMilliseconds.ToString(), Logger.Severity.Warning);

to scan the runningtime in the external text file, but how to clear the previous existed records in this text for every debugging?

Upvotes: 1

Views: 477

Answers (1)

Dennis Schröer
Dennis Schröer

Reputation: 2422

I think the easiest way is to just replace the text file if you truly want to clear the whole text file.

I don't know which platform you are working on, so here is an example in UWP:

logFile = await folder.CreateFileAsync("filename", CreationCollisionOption.ReplaceExisting);

For Logging I suggest you to not clear the text file because you will delete data you may need later. What I like to do is to create text file for each day. If there already is a text file for the current day, just open it and write. If it doesn't exist yet, create a new one (again UWP implementation):

string logDate = DateTime.Now.ToString("yyyy-MM-dd");
try
{
    Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
    string appName = package.DisplayName;
    LOG_FILENAME = appName + "_" + logDate + ".log";
}
catch
{
    // defaultname you defined somewhere else
    LOG_FILENAME += "_" + logDate + ".log";
}

StorageFolder folder = ApplicationData.Current.LocalFolder;

logFile = await folder.CreateFileAsync(LOG_FILENAME, CreationCollisionOption.OpenIfExists);

Upvotes: 1

Related Questions