Hi there
I have a program and when theres errors/starting up, etc. it logs to errors/into to files. i'm currently have an issue with it when sometimes when it tries to log the file it errors and says the file is in use. Need a way to fix it. here is the code i'm using
public static void LogMessageToFile(string Device, string logtype, string msg)
{
if (![login to view URL]("Thread was being aborted"))
{
if (![login to view URL]([login to view URL] + "\\Logs\\" + Device))
{
[login to view URL]([login to view URL] + "\\Logs\\" + Device);
}
if (logtype == "starting")
{
string strfilenames = "Log-" + [login to view URL]("yyyy-MM-dd");
StreamWriter Writer = null;
StringBuilder strbuilder = null;
string dir = [login to view URL] + "\\Logs\\" + Device + "\\";
if([login to view URL](dir))
{
[login to view URL](dir);
}
string logLine = [login to view URL]("{0:G}{1}", "", msg);
string path = [login to view URL](dir, strfilenames + ".log");
strbuilder = new StringBuilder(logLine);
Writer = new StreamWriter(path,true);
[login to view URL](strbuilder);
[login to view URL]();
}
else
{
string strfilenames = logtype + "-" + [login to view URL]("yyyy-MM-dd");
StreamWriter Writer = null;
StringBuilder strbuilder = null;
string dir = [login to view URL] + "\\Logs\\" + Device + "\\";
if ([login to view URL](dir))
{
[login to view URL](dir);
}
string logLine = [login to view URL]("{0:G}{1}", "", msg);
string path = [login to view URL](dir, strfilenames + ".log");
strbuilder = new StringBuilder(logLine);
Writer = new StreamWriter(path, true);
[login to view URL](strbuilder);
[login to view URL]();
}
}
}
Hello,
As an expert C# developer, i am very much interested to fix the issue. Can you plz send me the full project so that i can send back the solved version? Looking forward to have a positive response from you.
Thanks
Hello There!
I am a .net developer, I have built many applications in C#. I am readily available to provide my services to you. I have a good experience in handling files and maintaining the logs. I can show my work samples on demand.
We can discuss further on chat!
Kind regards:
Kamran
Hi. I'd like to give you my advices. First, If it is possible use library like log4net. Second, you can find process which opens log file using process explorer (use search by file handle). Third, realize class that will work with log files using singleton pattern.
Looks straightforward, you either have to maintain the lifetime/session of the file handle so that concurrent requests to log don't clash, or you could use a buffer.
Alternatively if you want something production ready then a free framework like NLog or log4net will do this with very little effort.
Cheers
I will fix the problem by rewriting the log function to Class and name it LoggingService and separate the old function to mini functions in that Class
,your problem is you are openning file for writting and not closing it and its difficult to find errors in long functions will multi responabilies
happy to help you