N
Nick Z.
Consider the following code:
class Log
{
private static StreamWriter w = null;
private static XmlTextWriter tw = null;
private void InitWriter()
{
w = new StreamWriter("log.xml", true, Encoding.Unicode);
tw = new XmlTextWriter(w);
}
private void Finalize()
{
if(tw!= null)
{
tw.Flush();
tw.Close();
}
}
public static void Report(string message)
{
if(tw == null)
InitWriter();
tw.WriteStartElement("message");
tw.WriteString(message);
tw.WriteEndElement();
}
}
Is it "wrong" to have a static stream open throughout the lifetime of a
class, and what are the implications of using the Finalize method? The
Finalize method is a serious performance hit as far as I know, but it
seems that its the right way to go here or not?
Opening the stream every time the Report method is called is much much
slower.
Thanks,
Nick Z.
class Log
{
private static StreamWriter w = null;
private static XmlTextWriter tw = null;
private void InitWriter()
{
w = new StreamWriter("log.xml", true, Encoding.Unicode);
tw = new XmlTextWriter(w);
}
private void Finalize()
{
if(tw!= null)
{
tw.Flush();
tw.Close();
}
}
public static void Report(string message)
{
if(tw == null)
InitWriter();
tw.WriteStartElement("message");
tw.WriteString(message);
tw.WriteEndElement();
}
}
Is it "wrong" to have a static stream open throughout the lifetime of a
class, and what are the implications of using the Finalize method? The
Finalize method is a serious performance hit as far as I know, but it
seems that its the right way to go here or not?
Opening the stream every time the Report method is called is much much
slower.
Thanks,
Nick Z.