Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# create log file

From the performance point of view your solution is not optimal. Every time you add another log entry with +=, the whole string is copied to another place in memory. I would recommend using StringBuilder instead:

StringBuilder sb = new StringBuilder();
...
sb.Append("log something");

...
// flush every 20 seconds as you do it
File.AppendAllText(filePath+"log.txt", sb.ToString());
sb.Clear();
By the way your timer event is probably executed on another thread. So you may want to use a mutex when accessing your sb object.

Another thing to consider is what happens to the log entries that were added within the last 20 seconds of the execution. You probably want to flush your string to the file right before the app exits.
Comment

PREVIOUS NEXT
Code Example
Csharp :: freeze scene unity 
Csharp :: wpf get dynamic resource from code 
Csharp :: how to use buildcontext in initstate flutter 
Csharp :: onmousedown() not working unity 
Csharp :: c# bootstrap checkbox 
Csharp :: render section asp.net mvc layout 
Csharp :: entity framework with query C# 
Csharp :: list of function in c# 
Csharp :: .net core change localhost port 
Csharp :: read all lines split C# 
Csharp :: C# How to make a field read-only outside of class 
Csharp :: even configuration custom errors page is not working asp.net MVC 
Csharp :: photon2 addcalbacktarget 
Csharp :: unitry raycast 
Csharp :: unity rotate around point 
Csharp :: c# retry delay request 
Csharp :: save position unity 
Csharp :: get script directory c# 
Csharp :: remove string inside curly braces C# 
Csharp :: Show empty message in data table angular material, If no data found 
Csharp :: unity find all scriptable objects of a type 
Csharp :: how to iterate string in c# 
Csharp :: reference a class by string unity 
Csharp :: JavaScriptSerializer() and convert to base64 
Csharp :: tilemap shader 
Csharp :: c# read huge file 
Csharp :: Severity Code Description Project File Line Suppression State Error MSB3021 
Csharp :: linq convert list to another list 
Csharp :: c# for loops 
Csharp :: datetimeoffset to datetime 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =