473,786 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does this look good?

cj
Public Class MyStringLogger
Private Shared m_loglock As New Object

Public Shared Sub Write(ByVal str As String)
SyncLock (m_loglock)
Dim sw As New System.io.Strea mWriter("c:\val idate.log",
True)
sw.WriteLine(st r)
sw.Close()
End SyncLock
End Sub
End Class

I have a class SessionClass in this program that gets instigated in it's
own thread when the program is running. Many of these instances might
be running at the same time each handling a TCP/IP communication
session. From within SessionClass I use:
MyStringLogger. Write("somethin g worthy of logging here") to add a line
to my log file.

It works but does anyone see any potential problems? Comments welcome.
Feb 23 '06 #1
11 1476
cj,

A potential problem I can see is that if your writing process to drive is
stucked for whatever reason, all the threads should wait (and do because
they are synclocked) until that writing is done. If that can give errors in
your application (and I thought that it was about scanning TCP ports)
because you miss something than you are in trouble.

I would use a Queue which is filled from the top (or visa versa) by the
threads. And synclocked when filling. The same synclock is used to get a
string from bottom and than the synclock is ended. The string is written
(not synclocked) to disk independent from the other processes.

http://msdn2.microsoft.com/en-us/lib...ons.queue.aspx

You need a timer to see everytime if there is something in your queue.

Be aware at the end of your program to empty the queue you have than to do
this as well.

I hope this helps,

Cor
Feb 23 '06 #2
Cor's comment about getting 'stuck' on the actual physical write to disk is
valid, however I would consider it to be low risk.

In my opinion, a more pressing potential problem would be the inability to
open the file for appending. This could happen if another process has the
file locked.

When I use this technique I work on the principle that if the operation
suceeds then well and and good but if it doesn't then I'm not going to lose
any sleep over it. To achieve this I code, within the Shared Sub:

SyncLock (m_loglock)
Dim sw As System.IO.Strea mWriter = Nothing
Try
sw = New System.I.Stream Writer("c:\vali date.log", True)
sw.Writeline(st r)
sw.Flush()
Catch
Finally
If sw IsNot Nothing Then sw.Close()
End Try
End SyncLock

This ensures that you don't get an exception on the Close if the open
failed. The Flush forces the buffer to be written to disk and can avoid
lines been written out of sequence when the Sub is called from differenrt
threads. I don't know how this happens but I have observed it occassionally.

I also tend to prepend the the log message with a timestamp down to
millisecond level, thus giving some rudimentary performance monitoring
information:

sw.Writeline("{ 0:HH:mm:ss.fff} {1}", DateTime.Now, str)

Note that the timestamp here is the time that the Writeline operation occurs
rather than the time of 'event' that caused the call to the Sub.

I suggest having a play with it, observing the results and tweaking it to
your own needs.
"cj" <cj@nospam.nosp am> wrote in message
news:e%******** ********@TK2MSF TNGP15.phx.gbl. ..
Public Class MyStringLogger
Private Shared m_loglock As New Object

Public Shared Sub Write(ByVal str As String)
SyncLock (m_loglock)
Dim sw As New System.io.Strea mWriter("c:\val idate.log",
True)
sw.WriteLine(st r)
sw.Close()
End SyncLock
End Sub
End Class

I have a class SessionClass in this program that gets instigated in it's
own thread when the program is running. Many of these instances might be
running at the same time each handling a TCP/IP communication session.
From within SessionClass I use: MyStringLogger. Write("somethin g worthy of
logging here") to add a line to my log file.

It works but does anyone see any potential problems? Comments welcome.

Feb 23 '06 #3
Hi Cj,
Welcome to MSDN Newsgroup!

I agree with Stephany's point here. You should call flush method to clear
the buffers for the file and cause all buffered data to be written to the
file. This will force any data remaining in the file buffer to be written
to the file. It helps protect you from some potential problems.

Thanks and have a nice day!

Best Regards,

Terry Fei [MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security

--------------------
Date: Thu, 23 Feb 2006 11:29:40 -0500
From: cj <cj@nospam.nosp am>
User-Agent: Thunderbird 1.5 (Windows/20051201)
MIME-Version: 1.0
Subject: Does this look good?
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <e#************ **@TK2MSFTNGP15 .phx.gbl>
Newsgroups: microsoft.publi c.dotnet.langua ges.vb
NNTP-Posting-Host: 208.254.170.98
Lines: 1
Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP15.phx. gbl
Xref: TK2MSFTNGXA01.p hx.gbl microsoft.publi c.dotnet.langua ges.vb:319203
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb

Public Class MyStringLogger
Private Shared m_loglock As New Object

Public Shared Sub Write(ByVal str As String)
SyncLock (m_loglock)
Dim sw As New System.io.Strea mWriter("c:\val idate.log",
True)
sw.WriteLine(st r)
sw.Close()
End SyncLock
End Sub
End Class

I have a class SessionClass in this program that gets instigated in it's
own thread when the program is running. Many of these instances might
be running at the same time each handling a TCP/IP communication
session. From within SessionClass I use:
MyStringLogger .Write("somethi ng worthy of logging here") to add a line
to my log file.

It works but does anyone see any potential problems? Comments welcome.


Feb 24 '06 #4
Stephany,
| Catch
| Finally
Be mindful of losing exceptions with empty Catch blocks. I would recommend:

| SyncLock (m_loglock)
| Dim sw As System.IO.Strea mWriter = Nothing
| Try
| sw = New System.I.Stream Writer("c:\vali date.log", True)
| sw.Writeline(st r)
| sw.Flush()
| Finally
| If sw IsNot Nothing Then sw.Close()
| End Try
| End SyncLock

The IsNot suggests VS 2005, I would recommend the Using statement instead,
see my other post.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhos t> wrote in message
news:Oe******** ******@TK2MSFTN GP14.phx.gbl...
| Cor's comment about getting 'stuck' on the actual physical write to disk
is
| valid, however I would consider it to be low risk.
|
| In my opinion, a more pressing potential problem would be the inability to
| open the file for appending. This could happen if another process has the
| file locked.
|
| When I use this technique I work on the principle that if the operation
| suceeds then well and and good but if it doesn't then I'm not going to
lose
| any sleep over it. To achieve this I code, within the Shared Sub:
|
| SyncLock (m_loglock)
| Dim sw As System.IO.Strea mWriter = Nothing
| Try
| sw = New System.I.Stream Writer("c:\vali date.log", True)
| sw.Writeline(st r)
| sw.Flush()
| Catch
| Finally
| If sw IsNot Nothing Then sw.Close()
| End Try
| End SyncLock
|
| This ensures that you don't get an exception on the Close if the open
| failed. The Flush forces the buffer to be written to disk and can avoid
| lines been written out of sequence when the Sub is called from differenrt
| threads. I don't know how this happens but I have observed it
occassionally.
|
| I also tend to prepend the the log message with a timestamp down to
| millisecond level, thus giving some rudimentary performance monitoring
| information:
|
| sw.Writeline("{ 0:HH:mm:ss.fff} {1}", DateTime.Now, str)
|
| Note that the timestamp here is the time that the Writeline operation
occurs
| rather than the time of 'event' that caused the call to the Sub.
|
| I suggest having a play with it, observing the results and tweaking it to
| your own needs.
|
|
| "cj" <cj@nospam.nosp am> wrote in message
| news:e%******** ********@TK2MSF TNGP15.phx.gbl. ..
| > Public Class MyStringLogger
| > Private Shared m_loglock As New Object
| >
| > Public Shared Sub Write(ByVal str As String)
| > SyncLock (m_loglock)
| > Dim sw As New System.io.Strea mWriter("c:\val idate.log",
| > True)
| > sw.WriteLine(st r)
| > sw.Close()
| > End SyncLock
| > End Sub
| > End Class
| >
| > I have a class SessionClass in this program that gets instigated in it's
| > own thread when the program is running. Many of these instances might
be
| > running at the same time each handling a TCP/IP communication session.
| > From within SessionClass I use: MyStringLogger. Write("somethin g worthy
of
| > logging here") to add a line to my log file.
| >
| > It works but does anyone see any potential problems? Comments welcome.
|
|
Feb 26 '06 #5
cj,
As Stephany shows, you should use a Try/Finally to ensure the file is
closed.

With VS 2005 you can use the new Using statement.
| Public Shared Sub Write(ByVal str As String)
| SyncLock (m_loglock)
Using sw As New System.io.Strea mWriter("c:\val idate.log",
| True)
| sw.WriteLine(st r)
End Using
| End SyncLock
| End Sub

Starting with VS 2005, I recommend the using statement, with VS 2002/2003 I
recommend a Try/Finally.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"cj" <cj@nospam.nosp am> wrote in message
news:e%******** ********@TK2MSF TNGP15.phx.gbl. ..
| Public Class MyStringLogger
| Private Shared m_loglock As New Object
|
| Public Shared Sub Write(ByVal str As String)
| SyncLock (m_loglock)
| Dim sw As New System.io.Strea mWriter("c:\val idate.log",
| True)
| sw.WriteLine(st r)
| sw.Close()
| End SyncLock
| End Sub
| End Class
|
| I have a class SessionClass in this program that gets instigated in it's
| own thread when the program is running. Many of these instances might
| be running at the same time each handling a TCP/IP communication
| session. From within SessionClass I use:
| MyStringLogger. Write("somethin g worthy of logging here") to add a line
| to my log file.
|
| It works but does anyone see any potential problems? Comments welcome.
Feb 26 '06 #6
Can you amplify on you comment about the 'empty Catch blocks' please Jay. I
don't really know what you mean.
"Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
message news:eX******** ******@TK2MSFTN GP09.phx.gbl...
Stephany,
| Catch
| Finally
Be mindful of losing exceptions with empty Catch blocks. I would
recommend:

| SyncLock (m_loglock)
| Dim sw As System.IO.Strea mWriter = Nothing
| Try
| sw = New System.I.Stream Writer("c:\vali date.log", True)
| sw.Writeline(st r)
| sw.Flush()
| Finally
| If sw IsNot Nothing Then sw.Close()
| End Try
| End SyncLock

The IsNot suggests VS 2005, I would recommend the Using statement instead,
see my other post.

--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhos t> wrote in message
news:Oe******** ******@TK2MSFTN GP14.phx.gbl...
| Cor's comment about getting 'stuck' on the actual physical write to disk
is
| valid, however I would consider it to be low risk.
|
| In my opinion, a more pressing potential problem would be the inability
to
| open the file for appending. This could happen if another process has
the
| file locked.
|
| When I use this technique I work on the principle that if the operation
| suceeds then well and and good but if it doesn't then I'm not going to
lose
| any sleep over it. To achieve this I code, within the Shared Sub:
|
| SyncLock (m_loglock)
| Dim sw As System.IO.Strea mWriter = Nothing
| Try
| sw = New System.I.Stream Writer("c:\vali date.log", True)
| sw.Writeline(st r)
| sw.Flush()
| Catch
| Finally
| If sw IsNot Nothing Then sw.Close()
| End Try
| End SyncLock
|
| This ensures that you don't get an exception on the Close if the open
| failed. The Flush forces the buffer to be written to disk and can avoid
| lines been written out of sequence when the Sub is called from
differenrt
| threads. I don't know how this happens but I have observed it
occassionally.
|
| I also tend to prepend the the log message with a timestamp down to
| millisecond level, thus giving some rudimentary performance monitoring
| information:
|
| sw.Writeline("{ 0:HH:mm:ss.fff} {1}", DateTime.Now, str)
|
| Note that the timestamp here is the time that the Writeline operation
occurs
| rather than the time of 'event' that caused the call to the Sub.
|
| I suggest having a play with it, observing the results and tweaking it
to
| your own needs.
|
|
| "cj" <cj@nospam.nosp am> wrote in message
| news:e%******** ********@TK2MSF TNGP15.phx.gbl. ..
| > Public Class MyStringLogger
| > Private Shared m_loglock As New Object
| >
| > Public Shared Sub Write(ByVal str As String)
| > SyncLock (m_loglock)
| > Dim sw As New
System.io.Strea mWriter("c:\val idate.log",
| > True)
| > sw.WriteLine(st r)
| > sw.Close()
| > End SyncLock
| > End Sub
| > End Class
| >
| > I have a class SessionClass in this program that gets instigated in
it's
| > own thread when the program is running. Many of these instances might
be
| > running at the same time each handling a TCP/IP communication session.
| > From within SessionClass I use: MyStringLogger. Write("somethin g worthy
of
| > logging here") to add a line to my log file.
| >
| > It works but does anyone see any potential problems? Comments
welcome.
|
|

Feb 26 '06 #7
The catch block will catch all exceptions, your catch block is empty, any
exceptions within the try block will "mysterious ly" disappear, possibly
hiding problems in your code.

Some developers refer to this phenonom as "swallowing exceptions", as your
catch block is swallows (eats) the exception.

http://msdn.microsoft.com/library/de...rp07192001.asp

http://blogs.msdn.com/ericgu/archive.../16/90712.aspx

When you swallow exceptions, higher level exceptions handlers do not have a
chance to respond to the exception, such as logging or preventing other
exceptions from happening.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhos t> wrote in message
news:uJ******** ********@TK2MSF TNGP11.phx.gbl. ..
| Can you amplify on you comment about the 'empty Catch blocks' please Jay.
I
| don't really know what you mean.
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
| message news:eX******** ******@TK2MSFTN GP09.phx.gbl...
| > Stephany,
| > | Catch
| > | Finally
| > Be mindful of losing exceptions with empty Catch blocks. I would
| > recommend:
| >
| > | SyncLock (m_loglock)
| > | Dim sw As System.IO.Strea mWriter = Nothing
| > | Try
| > | sw = New System.I.Stream Writer("c:\vali date.log", True)
| > | sw.Writeline(st r)
| > | sw.Flush()
| > | Finally
| > | If sw IsNot Nothing Then sw.Close()
| > | End Try
| > | End SyncLock
| >
| > The IsNot suggests VS 2005, I would recommend the Using statement
instead,
| > see my other post.
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Stephany Young" <noone@localhos t> wrote in message
| > news:Oe******** ******@TK2MSFTN GP14.phx.gbl...
| > | Cor's comment about getting 'stuck' on the actual physical write to
disk
| > is
| > | valid, however I would consider it to be low risk.
| > |
| > | In my opinion, a more pressing potential problem would be the
inability
| > to
| > | open the file for appending. This could happen if another process has
| > the
| > | file locked.
| > |
| > | When I use this technique I work on the principle that if the
operation
| > | suceeds then well and and good but if it doesn't then I'm not going to
| > lose
| > | any sleep over it. To achieve this I code, within the Shared Sub:
| > |
| > | SyncLock (m_loglock)
| > | Dim sw As System.IO.Strea mWriter = Nothing
| > | Try
| > | sw = New System.I.Stream Writer("c:\vali date.log", True)
| > | sw.Writeline(st r)
| > | sw.Flush()
| > | Catch
| > | Finally
| > | If sw IsNot Nothing Then sw.Close()
| > | End Try
| > | End SyncLock
| > |
| > | This ensures that you don't get an exception on the Close if the open
| > | failed. The Flush forces the buffer to be written to disk and can
avoid
| > | lines been written out of sequence when the Sub is called from
| > differenrt
| > | threads. I don't know how this happens but I have observed it
| > occassionally.
| > |
| > | I also tend to prepend the the log message with a timestamp down to
| > | millisecond level, thus giving some rudimentary performance monitoring
| > | information:
| > |
| > | sw.Writeline("{ 0:HH:mm:ss.fff} {1}", DateTime.Now, str)
| > |
| > | Note that the timestamp here is the time that the Writeline operation
| > occurs
| > | rather than the time of 'event' that caused the call to the Sub.
| > |
| > | I suggest having a play with it, observing the results and tweaking it
| > to
| > | your own needs.
| > |
| > |
| > | "cj" <cj@nospam.nosp am> wrote in message
| > | news:e%******** ********@TK2MSF TNGP15.phx.gbl. ..
| > | > Public Class MyStringLogger
| > | > Private Shared m_loglock As New Object
| > | >
| > | > Public Shared Sub Write(ByVal str As String)
| > | > SyncLock (m_loglock)
| > | > Dim sw As New
| > System.io.Strea mWriter("c:\val idate.log",
| > | > True)
| > | > sw.WriteLine(st r)
| > | > sw.Close()
| > | > End SyncLock
| > | > End Sub
| > | > End Class
| > | >
| > | > I have a class SessionClass in this program that gets instigated in
| > it's
| > | > own thread when the program is running. Many of these instances
might
| > be
| > | > running at the same time each handling a TCP/IP communication
session.
| > | > From within SessionClass I use: MyStringLogger. Write("somethin g
worthy
| > of
| > | > logging here") to add a line to my log file.
| > | >
| > | > It works but does anyone see any potential problems? Comments
| > welcome.
| > |
| > |
| >
| >
|
|
Feb 26 '06 #8
Yes of course - that goes without saying.

If you refer back to my post you will see that 'if the operation
suceeds then well and and good but if it doesn't then I'm not going to lose
any sleep over it'. In otherwords, if an exception occurs while writing the
line to the log file then I don't care. The empty 'Catch' block is included
specifically so that any exceptions thrown in the 'Try' block go down the
big black hole of nul. The conditional Close of the Streamwriter is designed
so that it does not fail if the StreamWriter object failed to instantiate.
"Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
message news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
The catch block will catch all exceptions, your catch block is empty, any
exceptions within the try block will "mysterious ly" disappear, possibly
hiding problems in your code.

Some developers refer to this phenonom as "swallowing exceptions", as
your
catch block is swallows (eats) the exception.

http://msdn.microsoft.com/library/de...rp07192001.asp

http://blogs.msdn.com/ericgu/archive.../16/90712.aspx

When you swallow exceptions, higher level exceptions handlers do not have
a
chance to respond to the exception, such as logging or preventing other
exceptions from happening.

--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhos t> wrote in message
news:uJ******** ********@TK2MSF TNGP11.phx.gbl. ..
| Can you amplify on you comment about the 'empty Catch blocks' please
Jay.
I
| don't really know what you mean.
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
| message news:eX******** ******@TK2MSFTN GP09.phx.gbl...
| > Stephany,
| > | Catch
| > | Finally
| > Be mindful of losing exceptions with empty Catch blocks. I would
| > recommend:
| >
| > | SyncLock (m_loglock)
| > | Dim sw As System.IO.Strea mWriter = Nothing
| > | Try
| > | sw = New System.I.Stream Writer("c:\vali date.log", True)
| > | sw.Writeline(st r)
| > | sw.Flush()
| > | Finally
| > | If sw IsNot Nothing Then sw.Close()
| > | End Try
| > | End SyncLock
| >
| > The IsNot suggests VS 2005, I would recommend the Using statement
instead,
| > see my other post.
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Stephany Young" <noone@localhos t> wrote in message
| > news:Oe******** ******@TK2MSFTN GP14.phx.gbl...
| > | Cor's comment about getting 'stuck' on the actual physical write to
disk
| > is
| > | valid, however I would consider it to be low risk.
| > |
| > | In my opinion, a more pressing potential problem would be the
inability
| > to
| > | open the file for appending. This could happen if another process
has
| > the
| > | file locked.
| > |
| > | When I use this technique I work on the principle that if the
operation
| > | suceeds then well and and good but if it doesn't then I'm not going
to
| > lose
| > | any sleep over it. To achieve this I code, within the Shared Sub:
| > |
| > | SyncLock (m_loglock)
| > | Dim sw As System.IO.Strea mWriter = Nothing
| > | Try
| > | sw = New System.I.Stream Writer("c:\vali date.log", True)
| > | sw.Writeline(st r)
| > | sw.Flush()
| > | Catch
| > | Finally
| > | If sw IsNot Nothing Then sw.Close()
| > | End Try
| > | End SyncLock
| > |
| > | This ensures that you don't get an exception on the Close if the
open
| > | failed. The Flush forces the buffer to be written to disk and can
avoid
| > | lines been written out of sequence when the Sub is called from
| > differenrt
| > | threads. I don't know how this happens but I have observed it
| > occassionally.
| > |
| > | I also tend to prepend the the log message with a timestamp down to
| > | millisecond level, thus giving some rudimentary performance
monitoring
| > | information:
| > |
| > | sw.Writeline("{ 0:HH:mm:ss.fff} {1}", DateTime.Now, str)
| > |
| > | Note that the timestamp here is the time that the Writeline
operation
| > occurs
| > | rather than the time of 'event' that caused the call to the Sub.
| > |
| > | I suggest having a play with it, observing the results and tweaking
it
| > to
| > | your own needs.
| > |
| > |
| > | "cj" <cj@nospam.nosp am> wrote in message
| > | news:e%******** ********@TK2MSF TNGP15.phx.gbl. ..
| > | > Public Class MyStringLogger
| > | > Private Shared m_loglock As New Object
| > | >
| > | > Public Shared Sub Write(ByVal str As String)
| > | > SyncLock (m_loglock)
| > | > Dim sw As New
| > System.io.Strea mWriter("c:\val idate.log",
| > | > True)
| > | > sw.WriteLine(st r)
| > | > sw.Close()
| > | > End SyncLock
| > | > End Sub
| > | > End Class
| > | >
| > | > I have a class SessionClass in this program that gets instigated
in
| > it's
| > | > own thread when the program is running. Many of these instances
might
| > be
| > | > running at the same time each handling a TCP/IP communication
session.
| > | > From within SessionClass I use: MyStringLogger. Write("somethin g
worthy
| > of
| > | > logging here") to add a line to my log file.
| > | >
| > | > It works but does anyone see any potential problems? Comments
| > welcome.
| > |
| > |
| >
| >
|
|

Feb 26 '06 #9
| In otherwords, if an exception occurs while writing the
| line to the log file then I don't care.
In which case I would recommend:

| > | > | Try
| > | > | sw = New System.I.Stream Writer("c:\vali date.log", True)
| > | > | sw.Writeline(st r)
| > | > | sw.Flush()
| > | > | Catch

' Ignore any exceptions that may occur

| > | > | Finally
| > | > | If sw IsNot Nothing Then sw.Close()
| > | > | End Try

This way developers reading your code, or inheriting you code, or even
yourself in 6 months. Know that you wanted exceptions ignored at this
point...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stephany Young" <noone@localhos t> wrote in message
news:OF******** ******@TK2MSFTN GP14.phx.gbl...
| Yes of course - that goes without saying.
|
| If you refer back to my post you will see that 'if the operation
| suceeds then well and and good but if it doesn't then I'm not going to
lose
| any sleep over it'. In otherwords, if an exception occurs while writing
the
| line to the log file then I don't care. The empty 'Catch' block is
included
| specifically so that any exceptions thrown in the 'Try' block go down the
| big black hole of nul. The conditional Close of the Streamwriter is
designed
| so that it does not fail if the StreamWriter object failed to instantiate.
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
| message news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
| > The catch block will catch all exceptions, your catch block is empty,
any
| > exceptions within the try block will "mysterious ly" disappear, possibly
| > hiding problems in your code.
| >
| > Some developers refer to this phenonom as "swallowing exceptions", as
| > your
| > catch block is swallows (eats) the exception.
| >
| >
http://msdn.microsoft.com/library/de...rp07192001.asp
| >
| > http://blogs.msdn.com/ericgu/archive.../16/90712.aspx
| >
| > When you swallow exceptions, higher level exceptions handlers do not
have
| > a
| > chance to respond to the exception, such as logging or preventing other
| > exceptions from happening.
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Stephany Young" <noone@localhos t> wrote in message
| > news:uJ******** ********@TK2MSF TNGP11.phx.gbl. ..
| > | Can you amplify on you comment about the 'empty Catch blocks' please
| > Jay.
| > I
| > | don't really know what you mean.
| > |
| > |
| > | "Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote
in
| > | message news:eX******** ******@TK2MSFTN GP09.phx.gbl...
| > | > Stephany,
| > | > | Catch
| > | > | Finally
| > | > Be mindful of losing exceptions with empty Catch blocks. I would
| > | > recommend:
| > | >
| > | > | SyncLock (m_loglock)
| > | > | Dim sw As System.IO.Strea mWriter = Nothing
| > | > | Try
| > | > | sw = New System.I.Stream Writer("c:\vali date.log", True)
| > | > | sw.Writeline(st r)
| > | > | sw.Flush()
| > | > | Finally
| > | > | If sw IsNot Nothing Then sw.Close()
| > | > | End Try
| > | > | End SyncLock
| > | >
| > | > The IsNot suggests VS 2005, I would recommend the Using statement
| > instead,
| > | > see my other post.
| > | >
| > | > --
| > | > Hope this helps
| > | > Jay [MVP - Outlook]
| > | > .NET Application Architect, Enthusiast, & Evangelist
| > | > T.S. Bradley - http://www.tsbradley.net
| > | >
| > | >
| > | > "Stephany Young" <noone@localhos t> wrote in message
| > | > news:Oe******** ******@TK2MSFTN GP14.phx.gbl...
| > | > | Cor's comment about getting 'stuck' on the actual physical write
to
| > disk
| > | > is
| > | > | valid, however I would consider it to be low risk.
| > | > |
| > | > | In my opinion, a more pressing potential problem would be the
| > inability
| > | > to
| > | > | open the file for appending. This could happen if another process
| > has
| > | > the
| > | > | file locked.
| > | > |
| > | > | When I use this technique I work on the principle that if the
| > operation
| > | > | suceeds then well and and good but if it doesn't then I'm not
going
| > to
| > | > lose
| > | > | any sleep over it. To achieve this I code, within the Shared Sub:
| > | > |
| > | > | SyncLock (m_loglock)
| > | > | Dim sw As System.IO.Strea mWriter = Nothing
| > | > | Try
| > | > | sw = New System.I.Stream Writer("c:\vali date.log", True)
| > | > | sw.Writeline(st r)
| > | > | sw.Flush()
| > | > | Catch
| > | > | Finally
| > | > | If sw IsNot Nothing Then sw.Close()
| > | > | End Try
| > | > | End SyncLock
| > | > |
| > | > | This ensures that you don't get an exception on the Close if the
| > open
| > | > | failed. The Flush forces the buffer to be written to disk and can
| > avoid
| > | > | lines been written out of sequence when the Sub is called from
| > | > differenrt
| > | > | threads. I don't know how this happens but I have observed it
| > | > occassionally.
| > | > |
| > | > | I also tend to prepend the the log message with a timestamp down
to
| > | > | millisecond level, thus giving some rudimentary performance
| > monitoring
| > | > | information:
| > | > |
| > | > | sw.Writeline("{ 0:HH:mm:ss.fff} {1}", DateTime.Now, str)
| > | > |
| > | > | Note that the timestamp here is the time that the Writeline
| > operation
| > | > occurs
| > | > | rather than the time of 'event' that caused the call to the Sub.
| > | > |
| > | > | I suggest having a play with it, observing the results and
tweaking
| > it
| > | > to
| > | > | your own needs.
| > | > |
| > | > |
| > | > | "cj" <cj@nospam.nosp am> wrote in message
| > | > | news:e%******** ********@TK2MSF TNGP15.phx.gbl. ..
| > | > | > Public Class MyStringLogger
| > | > | > Private Shared m_loglock As New Object
| > | > | >
| > | > | > Public Shared Sub Write(ByVal str As String)
| > | > | > SyncLock (m_loglock)
| > | > | > Dim sw As New
| > | > System.io.Strea mWriter("c:\val idate.log",
| > | > | > True)
| > | > | > sw.WriteLine(st r)
| > | > | > sw.Close()
| > | > | > End SyncLock
| > | > | > End Sub
| > | > | > End Class
| > | > | >
| > | > | > I have a class SessionClass in this program that gets instigated
| > in
| > | > it's
| > | > | > own thread when the program is running. Many of these instances
| > might
| > | > be
| > | > | > running at the same time each handling a TCP/IP communication
| > session.
| > | > | > From within SessionClass I use: MyStringLogger. Write("somethin g
| > worthy
| > | > of
| > | > | > logging here") to add a line to my log file.
| > | > | >
| > | > | > It works but does anyone see any potential problems? Comments
| > | > welcome.
| > | > |
| > | > |
| > | >
| > | >
| > |
| > |
| >
| >
|
|
Feb 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

162
7297
by: Isaac Grover | last post by:
Hi everyone, Just out of curiosity I recently pointed one of my hand-typed pages at the W3 Validator, and my hand-typed code was just ripped to shreds. Then I pointed some major sites (microsoft.com, cnn.com, etc.) at the W3 Validator; to my surprise none of them passed. Doesn't anyone care anymore, or are the standards more-or-less looked at as guidlines for web design?
13
5058
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
3
4413
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here really understand how this could be an issue. The assemblies that the system is complaining about are ones that we build here and we're not changing version numbers on anything. The errors come and go with no apparent rhyme or reason. We do not...
14
4863
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
55
6249
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.