writing to file from web service

C

cj2

Since a web service is basically multi-threaded. I was wondering how to
allow it to write to a file and not have all the threads tramping on
eachother.

I did it this way in a multi-threaded windows app I wrote recently.
Would this apply to a web service as well. It seems to work but it's
hard to say if problems will show under heavy load. Since this is for
logging when the web service can't write to it's primary log file (sql
server) it will only be used in a situation where sql is down. I hope
this code is never used but if it needs to be it must work.


from within the web service:

try
I do my sql insert
catch ex as exception
MyStringLogger.Write("\\fileserver\I\NewValLogs\" & Format(Now(),
"yyMMdd") & ".err", Now() & "|" & ResponseReasonCode & "|Save results
exception: " & ex.Message)
end try



Public Class MyStringLogger
Private Shared m_loglock As New Object

Public Shared Sub Write(ByVal fileName As String, ByVal strToWrite
As String)
SyncLock (m_loglock)
Try
Dim sw As New System.IO.StreamWriter(fileName, True)
sw.WriteLine(strToWrite)
sw.Close()
sw.Dispose()
Catch ex As Exception
End Try
End SyncLock
End Sub
end class
 
J

Jie Wang [MSFT]

Hi,

Not sure whether you were talking about a legacy ASP.NET web service or a
WCF web service. But the code just looks fine to me and it should work.

In addition, there are other choices, if you're interested:

You can also use the .NET built in Trace class to log messages to a text
file together with TextWriterTraceListener class. As stated in the MSDN
document, the Trace class is thread safe. The potential advantage of using
Trace class over your own is, it is configurable without changing the code.
More flexible when you want to change the log format (like txt to xml).

Windows Event logging is another option. The logs will be easier to
manage/analyze for admins.

If you have any futher questions, please don't hesitate to let me know.

Best regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jie Wang [MSFT]

Hi,

For ASP.NET Web Services, here is an article about Tracing for your
reference:

http://msdn.microsoft.com/en-us/library/bb885203.aspx

Regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Raja Venkatesh

Hi
The ASP.NET trace listener is a good trace for your logging.
The code given above also is good choice.
I've one more option for you to test which works very fine with no thread
contention and performance difference.

1. Create a structure/class with serialize property enabled. Keep atleast 1
property of this class with string of large size to store unexpected errors.
2. In the ASP.NET web service, have an array instance of this class in
shared mode stored in application session level.
3. Webservice will keep adding items to this shared array and call a
backgroundworker everytime.
4. In the dowork event of this backgroundworker, write one item at a time
from this array into text file and remove it from array.

This way this array works like a spool for log. You can prepare log of your
own custom data.

Points to consider here are the background worker should synclock during
remove operation and release immediately.
RV
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top