Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Am working in windows service using C#. I install the service and it working good and log file also working good.


I want to update the log file in daily basic. can anyone help it..

This is my code..

public static class Library
{
public static void writeErrorLog(string Message)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\Logfile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + " : " + Message);
sw.Flush();
sw.Close();
}
catch
{
}
}

}
Posted

1 solution

you can use following code if you want separate log files for each day

string vrCurrentDate = DateTime.Now.ToString("ddMMyyyy");
FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\" + vrCurrentDate + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(DateTime.Now.ToString() + " : " + Message);
sw.Flush();
sw.Close();



hope this will help you. "FileMode.OpenOrCreate" will create new file for each day if not created already otherwise it will update that existing file for that day
 
Share this answer
 
v2
Comments
MSVelu8788 25-May-15 3:13am    
Hi thanks for the quick reply. I try this code but it does not create the .txt file..
kashif Atiq 25-May-15 3:19am    
check for the rights on your service installed folder i just tried this code and its working fine for me it created "25052015.txt" file in my bin folder (as im running it from visual studio)
MSVelu8788 25-May-15 3:33am    
yes it working good and again thanks for your kindly and quick reply.. i have another doubt.

am using client and server as service. service running good but if client send a socket to server if it not receive means want to show an error message. is it possible....?. can you tel with an example code..
kashif Atiq 25-May-15 3:45am    
give me some time to gather some info for you on this. it will be better if you open another question for this so that other people can also help you on this

meanwhile please mark my answer as accepted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900