简体   繁体   中英

StreamWriter disposed when accessing method

I'm facing a problem where my declared StreamWriter gets disposed when I try to use a method to write on my log file. Everything is working as expected, except when I run AttachPink or AttachBlue , from another class. Then the StreamWriter is disposed and I get a nullPointerException

class Logs : IDisposable
    {

        //other declarations

        private StreamWriter HistoryWriter;
        private int ReportInterval = 0;

        public void NewHistory()
        {
            HistoryWriter = new StreamWriter(HistoryLocation + HistoryName + HistoryExtension);
            PrepareHistory();
        }

        private void PrepareHistory()
        {
            HistoryWriter.WriteLine("<html><body bgcolor='#000000'>");
            /*
             *  Insert initial HTML tags 
             */
        }

        public void SendHistory()
        {
            HistoryWriter.WriteLine("</body></html>");
            /*
             *  Final HTML tags
             */ 
            HistoryWriter.Close();
            if (ReportInterval > 0)
            {
                /*
                 *  Upload File
                 */
            }
            else
            {
                Debug.WriteLine("ERROR: Report Interval for History has not been set");
            }
            NewHistory();
        }

        public void AttachPink(String message, StreamWriter writer)
        {
            writer.Write(
                "<font color='DA1BE0'>" 
                + message
                + "</font>");
        }

        public void AttachBlue(String message, StreamWriter writer)
        {
            writer.Write(
                "<font color='0C93ED'>" 
                + message
                + "</font>");
        }

        public StreamWriter getHistoryWriter()
        {
            return HistoryWriter;
        }

        public void SetHistoryInterval(int interval)
        {
            ReportInterval = interval;
        }

        public void Dispose()
        {
            if (HistoryWriter != null)
            {
                HistoryWriter.Close();
                HistoryWriter.Dispose();
                HistoryWriter = null;
            }
        }

    }

To use the methods I simply declare an instance of Logs class inside another class, like so:

class UsingLogs
    {
        Logs logs = new Logs();
        logs.NewHistory();
        logs.AttachBlue("my message", logs.getHistoryWriter());
    }

I don't know how should I go for preserving classes variables state when accessing multiple methods.

I guess what you are looking for is the Singleton Pattern ( http://en.wikipedia.org/wiki/Singleton_pattern )

a simple implementation of mine which you can reuse every time you need a singleton

public class Singleton<T> where T : class, new()
{
    private static object sync = null;
    private static volatile T i;
    protected Singleton() { }

    public static T I
    {
        get
        {
            if (i == null)
                lock (sync)
                    if (i == null)
                        i = new T();

            return i;
        }
    }
}

You can implement your Log class like this:

class Logs : Singleton<Logs>
{
... your code goes here
}

In your code, when you want to use the Logs class, you simply use:

Logs.I.AttachBlue(...);

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM