这看起来不错吗? [英] Does this look good?

查看:104
本文介绍了这看起来不错吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Public Class MyStringLogger

私有共享m_loglock作为新对象

公共共享子写(ByVal str As String)

SyncLock(m_loglock)

Dim sw As New System.io.StreamWriter(" c:\ validate.log",

True)

sw.WriteLine(str)

sw.Close()

结束SyncLock

结束次级

结束班级


我在这个程序中有一个类SessionClass,当程序运行时,它会在它的'b
自己的线程中发起。其中许多实例可能会在每个处理TCP / IP通信的同时运行
会话。在SessionClass中我使用:

MyStringLogger.Write(值得记录的东西)在我的日志文件中添加一行




它有效但有没有人看到任何潜在的问题?欢迎评论。

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.StreamWriter("c:\validate.log",
True)
sw.WriteLine(str)
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("something worthy of logging here") to add a line
to my log file.

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

推荐答案

cj,


我能看到的一个潜在问题是,如果你的写作过程无论出于何种原因,所以线程应该等待(并且因为

它们是同步的)直到写完为止。如果这可能会给你的应用程序带来错误(我认为它是关于扫描TCP端口的)

因为你错过了一些东西而不是你遇到麻烦。


我会使用一个由顶部填充的队列(反之亦然)由

线程填充。并在填充时同步。同一个synclock用于从底部获取一个

字符串,并且synclock结束。该字符串写入

(未同步)到磁盘独立于其他进程。

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


如果队列中有东西,你需要一个计时器才能看到。


在你的程序结束时要注意清空队列这也不仅仅是这个。




我希望这会有所帮助,


Cor


Cor'关于在实际物理写入磁盘上卡住的评论是

有效,但我认为它是低的风险。


在我看来,一个更紧迫的潜在问题是无法打开文件进行追加。如果另一个进程锁定了

文件,就会发生这种情况。

当我使用这种技术时,我的工作原则是如果操作

然后很好而且很好但是如果它不会那么我将不会失去

任何睡眠。为了达到这个目的,我在Shared Sub中编码:


SyncLock(m_loglock)

Dim sw As System.IO.StreamWriter = Nothing

尝试

sw = New System.I.StreamWriter(" c:\ validate.log",True)

sw.Writeline(str)

sw.Flush()

Catch

最后

如果sw IsNot Nothing那么sw.Close()

结束尝试

结束SyncLock


这样可确保您在关闭时不会出现异常(如果打开)

失败了。 Flush强制将缓冲区写入磁盘,并且可以避免在从differenrt

线程调用Sub时不按顺序写入
行。我不知道这是怎么发生的,但我偶尔会观察到它。


我也倾向于在时间戳前面添加日志消息

毫秒级,从而提供一些基本的性能监控

信息:


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


请注意,这里的时间戳是Writeline操作发生的时间

而不是''b'的时间导致调用Sub的事件'


我建议玩它,观察结果并将其调整为你自己需要的


" cj" < cj@nospam.nospam>在消息中写道

news:e%**************** @ TK2MSFTNGP15.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.StreamWriter = Nothing
Try
sw = New System.I.StreamWriter("c:\validate.log", True)
sw.Writeline(str)
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.nospam> wrote in message
news:e%****************@TK2MSFTNGP15.phx.gbl...
公共类MyStringLogger
私有共享m_loglock作为新对象

公共共享子写(ByVal str As String)
SyncLock(m_loglock)
Dim sw作为新的System.io.StreamWriter (" c:\ validate.log",
True)
sw.WriteLine(str)
sw.Close()
结束SyncLock
End Sub <结束语

我在这个程序中有一个类SessionClass,当程序运行时,它会在它自己的线程中发起。其中许多实例可能在每个处理TCP / IP通信会话的同时运行。
在SessionClass中我使用:MyStringLogger.Write(值得在这里记录的东西) )在我的日志文件中添加一行。

它有效,但有没有人看到任何潜在的问题?欢迎评论。
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.StreamWriter("c:\validate.log",
True)
sw.WriteLine(str)
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("something worthy of
logging here") to add a line to my log file.

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



嗨Cj,

欢迎来到MSDN新闻组!


我同意斯蒂芬妮的观点。您应该调用flush方法来清除文件的缓冲区,并将所有缓冲的数据写入

文件。这将强制文件缓冲区中剩余的任何数据写入文件
。它有助于保护您免受一些潜在问题的影响。


谢谢你,祝你有愉快的一天!


最诚挚的问候,

Terry Fei [MSFT]

微软社区支持

安全! www.microsoft.com/security


--------------------
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

--------------------
日期:星期四,2006年2月23日11:29:40 -0500
来自:cj < cj@nospam.nospam>
用户代理:Thunderbird 1.5(Windows / 20051201)
MIME版本:1.0
主题:这看起来不错吗?
内容类型:text / plain;字符集= ISO-8859-1; format = flowed
Content-Transfer-Encoding:7bit
消息ID:< e#************** @ TK2MSFTNGP15.phx.gbl>
新闻组:microsoft.public.dotnet.languages.vb
NNTP-Posting-Host:208.254.170.98
行:1
路径:TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
外部参照:TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:319203
X-Tomcat-NG:microsoft.public.dotnet.languages.vb

公共类MyStringLogger
私有共享m_loglock作为新对象

公共共享子写(ByVal str As String)
SyncLock(m_loglock)
Dim sw As新的System.io.StreamWriter(" c:\ validate.log",
True)
sw.WriteLine(str)
sw.Close()
结束SyncLock <结束Sub
End Class

我在这个程序中有一个类SessionClass,它在程序中引发了它自己的线程我在跑。其中许多实例可能在每个处理TCP / IP通信会话的同时运行。在SessionClass中我使用:
MyStringLogger.Write(值得记录的东西)在我的日志文件中添加一行


它有效,但有人看到任何潜在的问题?欢迎评论。
Date: Thu, 23 Feb 2006 11:29:40 -0500
From: cj <cj@nospam.nospam>
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.public.dotnet.languages.vb
NNTP-Posting-Host: 208.254.170.98
Lines: 1
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:319203
X-Tomcat-NG: microsoft.public.dotnet.languages.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.StreamWriter("c:\validate.log",
True)
sw.WriteLine(str)
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("something worthy of logging here") to add a line
to my log file.

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






这篇关于这看起来不错吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆