2

I have a byte[] with some data in it, I would like to write this byte array AS-IS to the log file using log4.net. The problems that i am facing is that

There are no overload for byte[] in TextWriter, so even implementing an IObjectRenderer is of no use. I dont have access to the underlying Stream object of Log4.net Also tried converting byte[] into char[] still when i write it, it adds an extra byte.

Is this even possible with Log4.net.

Thanx in Advance.

like.no.other
  • 95
  • 2
  • 10

4 Answers4

4

Log files are usually plain text files. It's probably best to log your byte array represented as string.

Have a look at BitConverter.ToString or Convert.ToBase64String.

dtb
  • 213,145
  • 36
  • 401
  • 431
  • the only problem with this approach is that the log file would be double the size and also not very readable as the data contains a mix of human readable string and binary info. – like.no.other Apr 13 '10 at 21:26
  • 3
    Then don't write it to the log file :-) Log files are for diagnostic messages, not piles of binary data. – dtb Apr 13 '10 at 21:30
3

Nope. Have you thought about writing it out as a hex string (see this post)?

Community
  • 1
  • 1
Tom Cabanski
  • 7,828
  • 2
  • 22
  • 25
  • the only problem with this approach is that the log file would be double the size and also not very readable as the data contains a mix of human readable string and binary info. – like.no.other Apr 13 '10 at 21:27
  • If you know how to write out HEX string, you can feel free to write out other string patterns if you like. – Lex Li Apr 14 '10 at 06:55
0

If you are logging into DB then use Binary type with maximum size

csensoft
  • 493
  • 1
  • 6
  • 15
0

I also think that logging any larger data is kind of useless, however, i guess this is what you are looking for - this converts your bytes to string.

 System.Text.Encoding.ASCII.GetString(byteArray)

I believe you can figure out how to use that for logging.

Pz, the TaskConnect developer

Pz.
  • 1,119
  • 10
  • 14