在其生命周期结束时刷新 StreamWriter [英] Flush StreamWriter at the end of its lifetime

查看:41
本文介绍了在其生命周期结束时刷新 StreamWriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似单例的类,可以做一些日志输出:

I have a singleton-like class that can do some logging output:

class Foo
{
    private static Foo instance;
    private System.IO.StreamWriter os;

    private Foo()
    {
        this.os = System.IO.File.CreateText( "D:/tmp/test" );
    }

    public static Foo Instance
    {
        get
        {
            if ( instance == null )
                instance = new Foo();
            return instance;
        }
    }

    public void Log( string s )
    {
        os.Write( s );
    }
}

当我在像

class Program
{
    private static void Main( string[] args )
    {
        Foo.Instance.Log( "asdf\n" );
    }
}

正在创建文件,但没有写入输出.我认为这是因为 StreamWriter 从未被刷新.

the file is being created, but no output is written. I assume this is because the StreamWriter has never been flushed.

我试图通过调用 ~Foo() 中的 Close() 来修复类:

I tried to repair the class by calling to Close() in ~Foo():

~Foo()
{
    os.Close();
}

但这会产生一个 ObjectDisposedException.显然,当 Foo 的析构函数被调用时,Foo.os 已经被释放了.

but this yields a ObjectDisposedException. Apparently Foo.os has already been disposed when Foo's destructor is called.

如何确保我的 StreamWriter 终于"被刷新?

How do I ensure that my StreamWriter is flushed "at last"?

编辑

设置 this.os.AutoFlush = true; 有效.将 Flush() 方法添加到 Foo 并在适当的地方调用它也是如此,但我很感兴趣,如果没有任何方法可以做到.

Setting this.os.AutoFlush = true; works. Adding a Flush() method to Foo and calling it in appropiate places does as well, but I'm interested if there any way of doing without.

推荐答案

首先,使用单例本身就会产生问题,这不需要另外的证明.在这里,它是一个伪装的全局清理.StreamWriter 不会在程序结束时自动刷新,并且根据 文档

First of all, using a singleton creates problems in its own right, and this did not need another proof. Here, it's cleanup for a disguised global. The StreamWriter does not auto-flush on program end and according to the documentation,

您必须调用 Close 以确保所有数据都正确写出到底层流.

You must call Close to ensure that all data is correctly written out to the underlying stream.

感谢@PeterDuniho 对Self-closed StreamWriter singleton"的回答解决方案可能是将构造函数更改为

Thanks to an answer to "Self-closing StreamWriter singleton" from @PeterDuniho a possible solution could be changing the constructor to

private Foo()
{
    this.os = System.IO.File.CreateText( "D:/tmp/test" );
    System.AppDomain.CurrentDomain.ProcessExit +=
        (sender, eventArgs) => this.os.Close();
}

考虑到在析构函数中调用 Close() 的问题,我不应该忽略到处写的终结器无论如何都没有多大用处".在这种情况下,由于垃圾回收没有使用特定的顺序,StreamWriter 对象已经被回收,并且不能在其复活的僵尸状态下关闭.

Considering the problem of calling Close() in the destructor, I should not have ignored the "finalizers are not of much use anyway" written all over the place. In this case, as garbage collection does not use a specific order, the StreamWriter object has already been collected and cannot be closed in its resurrected zombie state.

这篇关于在其生命周期结束时刷新 StreamWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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