0

I´ve a binarywriter with a memorystream, and I need to clear it if one condition happens:

public void Execute() {
    MemoryStream memoryStream = new MemoryStream();
    BinaryWriter writer = new BinaryWriter(memoryStream);
    Log log = new Log(writer);
    log.Write("OK1");
    ...

    // If condition
    memoryStream = new MemoryStream();
    ...
    log.Write("OK2");
    ...
}

public class Log {

    private BinaryWriter log;

    public Log(BinaryWriter bw)
    {
        log = bw;
    }

    public void write(string msg)
    {
        log.Write(msg);
    }
}

But the problem is memorystream is empty. I ´ve tried creating a new BinaryWriter again:

public void Execute() {
    MemoryStream memoryStream = new MemoryStream();
    BinaryWriter writer = new BinaryWriter(memoryStream);
    Log log = new Log(writer);
    log.Write("OK1");
    ...

    // If condition
    memoryStream = new MemoryStream();
    writer = new BinaryWriter(memoryStream);
    ...
    log.Write("OK2");
    ...
}

public class Log {

    private BinaryWriter log;

    public Log(BinaryWriter bw)
    {
        log = bw;
    }

    public void write(string msg)
    {
        log.Write(msg);
    }
}

But both BinaryWriter and MemoryStream are empry.

How can I clear binarywriter with memorystream? I need to write the final content of memorystream to a file.

Thanks!

Alberto
  • 704
  • 2
  • 12
  • 26
  • You are creating the binary writer on `recordStream`, not on `memoryStream`. – Anton Tykhyy Dec 01 '13 at 14:14
  • Sorry, I renamed the variables in this post to make it more clear. I´ve fixed it in the code. I think is easier to identify memorystream to MemorySteam. In my code there is only one MemoryStream and one BinaryWriter, so no problem with other references :) – Alberto Dec 01 '13 at 14:24

3 Answers3

1

This code initilize log object by a memory stream and after that create another memory stream that its reference stored in memoryStream, but log object binarywriter has its original memory stream, not new memory stream. You can check this by checking HashCode of them.

AlirezaJ
  • 111
  • 6
1

Do you mean you want to write to a different MemoryStream? You have passed the reference of the old writer to the Log so the Log instance holds the reference. Altering the writer variable after that won't take effect. You need to either

  1. Create a new Log and pass the new writer to it
  2. Expose a method of Log to allow manipulation of the BinaryWriter kept by the object from outside
Gildor
  • 2,484
  • 23
  • 35
  • Yes, you´re right, of courrse. I´ve created a new Log object after creating new BinaryWriter and MemoryStream (althought I could have created a new method in Log called reset with a BinaryWriter as argument...). – Alberto Dec 01 '13 at 14:47
1

The first snippet doesn't work because the BinaryWriter is still using your old MemoryStream reference. You cannot convince it to use the new one.

The second snippet doesn't work because your Log class is still using your old BinaryWriter reference. You could convince it to use the new one by adding a method that updates "log".

Or you can cheat and call memoryStream.SetLength(0) to force it to start back at the beginning.

Do review the wisdom of using a MemoryStream for logging. There's no upper bound on how large it is going to get. Presumably the reason you are looking for this workaround. Completely throwing everything away when you need to rewind the stream is not pretty. Murphy will ensure that this happens at the exact moment in time when you need the info from the log to troubleshoot a problem. A circular buffer doesn't have this problem.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • This is not a common log. I´ve siemply it to this post. In my project, I Serialize a whole Microsoft class, so BinaryWriter is needed. But your answer is all rihgt :) – Alberto Dec 01 '13 at 14:50