Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am running a web service that can accept multiple connections at once (asynchronously). In this web service, I am attempting to write a line of text to a log file, which works some of the time. However, after a certain number of writes, it breaks (editor's note: OP needs to clarify what breaks and how).

The below code is what I am using to write to the log file. I have tried adding/removing fs.Close() and fs.Flush(), but I still get the same error (editor's note: OP should include the error message).

Please keep in mind that I am a CS student who is an intern, so I am a little inexperienced (editor's note: be gentle). I was told I could use this site if I needed help with anything. Thanks in advance!

C#
using (FileStream fs = new FileStream(string.Format(@"{0}\Information logs\{1}",
    Properties.Settings.Default.InformationLogsDestination, logName),
    FileMode.Append, FileAccess.Write))
{
    StreamWriter w = new StreamWriter(fs, Encoding.UTF8);
    w.WriteLine(DateTime.Now.ToString("G") + " posting to '" +
        message + "' to list " + name + " in Database.");
    w.Flush();
    w.Close();
    fs.Close();
}
Posted
Updated 26-Jul-11 13:28pm
v4

Alternatively, write to a database or the event log rather than a file.
 
Share this answer
 
Comments
Member 8015046 26-Jul-11 20:01pm    
I could try to do the database way. How do you do the event log way?
AspDotNetDev 26-Jul-11 20:36pm    
FYI, I added a comment below. A Code Project bug prevented me from seeing (and so, replying) to your comment until I posted another comment.
Philippe Mori 27-Jul-11 7:40am    
Similar with me. Yesterday, I was not able to see comments (for any solution related to this question) even I have received an email.
AspDotNetDev 26-Jul-11 20:35pm    
Write to event log from web service: http://aspalliance.com/987_Event_Logging_in_a_NET_Web_Service.all
Member 8015046 27-Jul-11 11:06am    
Why is an event log preferable than a text document?
C#
lock(myStaticVariable)
{
    // Code to write to file.
}


Use a static variable to lock on and ensure no two threads are writing to the same file at once.
 
Share this answer
 
Comments
Member 8015046 27-Jul-11 11:11am    
I tried doing that and it still came to the same problem. But like the idea very much. Here is how I implemented it:
Member 8015046 27-Jul-11 11:11am    
Boolean post = PostDB(name, message);
if (Properties.Settings.Default.Trace)
{
lock (llock)
{
using (FileStream fs = new FileStream(string.Format(@"{0}\Information logs\{1}", Properties.Settings.Default.InformationLogsDestination, logName), FileMode.Append, FileAccess.Write))
{
StreamWriter w = new StreamWriter(fs, Encoding.UTF8);
w.WriteLine(DateTime.Now.ToString("G") + " posting to '" + message + "' to list " + name + " in Database.");
w.Flush();
w.Close();
}
}
}
return post;
Member 8015046 27-Jul-11 11:12am    
static readonly object llock = new object();
AspDotNetDev 27-Jul-11 11:18am    
Hmmm, perhaps try flusing the FileStream in addition to the StreamWriter.
Member 8015046 27-Jul-11 11:22am    
Yeah I try many combinations of those. It still breaks on me.
Each program should uses its own file... or the web service should do all the logging.

It is preferable to avoid using the same file remotly particulary if they are used a lot.

Given the above code, if one program open the file and a second one try to open it before the first program has finished, then an exception will be raised since the file is lock by the first program.

Thus a simple solution if you still think that using a single file would be to catch the appropriate exception and retry the operation a bit later.

By the way such program won't scale well. Thus that solution could be adequat only for a few applications that do not uses the log intensively.
 
Share this answer
 
Comments
Member 8015046 26-Jul-11 19:12pm    
Well both programs calls a library which calls this web service. This web service is the one doing the logging. So the web service is being called multiple times. What do you suggest I should do?

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