Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How do i write in a common file from separate projects?
i create TextWriter in Initializing each project, then write logs in whole of projects.
_Writer = TextWriter.Synchronized(new StreamWriter(path));


when it needs to write log, i call:
_Writer.WriteLine(line);
     _Writer.Flush()


but when first project runs, second one cann't create TextWriter then it(second project) doesn't write any thing in common log file.

How do i create TextWriter object in share writing mode for file in common path ?
Posted
Comments
Richard MacCutchan 15-Oct-14 3:36am    
You would need a separate application that writes the file, and other projects would connect to it via sockets or pipes.
NaibedyaKar 15-Oct-14 3:37am    
If you want to use same logging feature on all of your projects, then better to create a separate application and use the assembly on all your projects.

Don't hold files open - particularly log files, but generally it's a bad idea to hold them open anyway.
Instead, do your logging like this:
C#
File.AppendAllText(logFilePath, "String to log\n");

See here: http://msdn.microsoft.com/en-us/library/ms143356(v=vs.110).aspx[^]

You may want to put this in a retry loop and a try...catch block just to be on the safe side if you have multiple apps writing to the same log file.
 
Share this answer
 
Comments
Zon-cpp 15-Oct-14 4:43am    
Thanks,
dose the 'File.AppendAllText()' synchronize?
how do i handle multi-threading project? with Lock()?
This is not an appropriate or reliable way for writing logs. Instead, you can use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog%28v=vs.110%29.aspx[^].

Another option is using Apache log4net: http://logging.apache.org/log4net[^].

—SA
 
Share this answer
 
Comments
Zon-cpp 15-Oct-14 8:24am    
by EventLog, can i specify a special path beside the exe file?
Sergey Alexandrovich Kryukov 15-Oct-14 11:43am    
This is not how it works. If you need to customize EventLog, you can do it. Start from reading MSDN documentation thoroughly, ask questions if you face any problems.
—SA
Sergey Alexandrovich Kryukov 15-Oct-14 11:51am    
Please see Solution 5 for further detail.
I recommend to use default EventLog behavior, but you decide.
Consider accepting these answers formally (green "Accept" button). EventLog is the best approach to writing logs in a unified reliable way.
—SA
Zon-cpp 16-Oct-14 4:31am    
my meaning is : can i specify a special path for EventLog, beside the path of exe file?
actually i used OriginalGriff1 's solution. thanks
Sergey Alexandrovich Kryukov 16-Oct-14 12:17pm    
Whatever. I answered about the "special path" in Solution 5.
But using EventLog is better, by a number of reasons.
—SA
Zon-cpp asked:

by EventLog, can i specify a special path beside the exe file?
Who told you that the "path" is "exe file". This is not how it works.

You can customize EventLog to change the sink of data, to make it a file or anything else, but I would recommend to use default behavior. To understand how, please see my past answer: MsBuild OutPut to the TextBox on the fly in Windows Application[^].

Even if you use default sink, system event log, to be viewed by the standard Event Log viewer, you can register (and later unregister) your custom event source, to have your events stored in a separate application-specific node of the event tree. Please see my past answer: How to create event log under a folder[^].

—SA
 
Share this answer
 
Comments
[no name] 15-Oct-14 14:54pm    
Helps a lot, also for me :) My 5. Regards, Bruno
Sergey Alexandrovich Kryukov 15-Oct-14 15:36pm    
Thank you, Bruno.
—SA
As I said in the comment, you can create a separate application to do the logging with the below code and then use the assembly of that application on all other projects to do the logging for you.

C#
public void WriteLog(string logMessage, string logErrorPath)
        {
            StreamWriter log;
            string filePath = logErrorPath + "\\AppName" + DateTime.Now.ToString("dd_MM_yy") + ".log";

            if (!File.Exists(filePath))
            {
                log = new StreamWriter(filePath);
            }
            else
            {
                log = File.AppendText(filePath);
            }

            // Write to the file:
            log.WriteLine(DateTime.Now.ToString() + ": " + logMessage);          
            // Close the stream:
            log.Close();
        }
 
Share this answer
 
Comments
BillWoodruff 15-Oct-14 3:58am    
What happens when two applications both try to write to the log file at the same time ?
NaibedyaKar 15-Oct-14 4:08am    
It will not affect it as each application will create it's own instance.

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