I am having a problem with my StreamWriter. I'm using Visual Studion 2010 and 2015, .NET Framework 4.0. Here is the sample of my Code to write a log file:
using (StreamWriter swErr = new StreamWriter(HostingEnvironment.MapPath("~/Logs/") +
DateTime.Now.ToString("yyyyMMdd") + ".log", true, Encoding.Default))
{
if (!string.IsNullOrEmpty(errMsg))
swErr.WriteLine(DateTime.Now.ToString("HH:mm:ss " + errMsg));
if (result != null && result.Count > 0)
foreach (Mandrill.EmailResult x in result)
{
stat = string.Format("{0} | {1} | {2} | {3}",
x.Id, x.Email, x.Status, x.RejectReason ?? "-"
);
Console.WriteLine(stat);
swErr.WriteLine(DateTime.Now.ToString("HH:mm:ss " + stat));
}
if (!string.IsNullOrEmpty(errMsg) || (result != null && result.Count > 0))
swErr.WriteLine("---");
}
For some reason, here is the sample Expected vs Actual output of this code:
Expected OUTPUT:
10:10:02 e7424f10d1ee431fb69d9014265b278a | tito.genovajr@paramount.com.ph | Sent
----------
ACTUAL OUTPUT
10:10:02 e742431091ee4313b6999014265b278a | AiAo.A.D.enovajr@para10ounA.co10.p10 | SenA
----------
For some reason I do not know it converts some character to a random number or letter(s). I tried removing the parameter of StreamWriter for Encoding, also tried passing Encoding.ASCII
and Encoding.UTF8
but it's still the same.
I think my syntax is OK and handled the disposing of the StreamWriter properly.
I hope anyone can help me regarding this.
Thanks!
For the ones who will encounter this question. I'm really sorry for my stupidity but this is a syntax error. The error message is included on the string formatting of the date.
DateTime.Now.ToString("HH:mm:ss" + errMsg);
This must be:
DateTime.Now.ToString("HH:mm:ss") + errMsg;
Hope any other person would not encounter this.