C# 传递给方法的Memorystream对象不';不包含值
我正在使用memorystream对象将streamwriter对象中的值写入内存。我有几种方法用于记录错误和值,因此我将memorystream对象作为参数在它们之间传递 我的问题是memorystream不包含值。见下面的代码:C# 传递给方法的Memorystream对象不';不包含值,c#,asp.net,memorystream,streamwriter,C#,Asp.net,Memorystream,Streamwriter,我正在使用memorystream对象将streamwriter对象中的值写入内存。我有几种方法用于记录错误和值,因此我将memorystream对象作为参数在它们之间传递 我的问题是memorystream不包含值。见下面的代码: #region csvlogging public static void initiateCsvLogging(SortedList<int, string> logList, SortedList<int, string>
#region csvlogging
public static void initiateCsvLogging(SortedList<int, string> logList, SortedList<int, string> errorList) // Handles csv upload logging
{
logMsgError(errorList, logList);
}
public static void logMsgError(SortedList<int, string> errorList, SortedList<int, string> logList)
{
using (MemoryStream ms = new MemoryStream())
{
StreamWriter sw = new StreamWriter(ms);
try
{
sw.WriteLine("Errors:");
foreach (KeyValuePair<int, string> k in errorList)
{
if (k.Value == null)
{
sw.WriteLine("No errors reported.");
}
else
{
sw.WriteLine(k.Key + "," + k.Value);
}
}
}
catch (Exception ex)
{
}
finally
{
sw.WriteLine("");
}
logMsg(logList, ms);
}
} // Handles data upload error logging for csv
public static MemoryStream logMsg(SortedList<int, string> logList, MemoryStream ms)
{
string DateNow = System.DateTime.Now.Date.ToString("yyyy-MM-dd");
string FileName = "log " + DateNow + ".csv";
char[] delimiterChars = { ',' };
try
{
// Write values to textfile and save to Log folder
using (StreamWriter sw = new StreamWriter(ms))
{
if (logList.Keys.Count == 0)
{
sw.WriteLine("No new users added to Active Directory.");
}
else // If keys > 0 then new users have been added to AD
{
sw.WriteLine("Username" + "," + "Password" + "," + "Company" + "," + "Email");
foreach (KeyValuePair<int, string> k in logList)
{
string[] values = k.Value.Split(delimiterChars);
sw.WriteLine(values[0] + "," + values[1] + "," + values[2] + "," + values[3]);
}
}
try
{
sw.Flush();
sendLogByEmail(new MemoryStream(ms.ToArray()), FileName);
}
catch (Exception ex)
{
}
}
}
catch (ThreadAbortException ex)
{
}
finally
{
}
return ms;
} // Handles logging for csv
public static void sendLogByEmail(MemoryStream ms, string error, int count)
{
//Send mail by attachment code
SmtpClient smtpclient = new SmtpClient();
//smtpclient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
//smtpclient.UseDefaultCredentials = true;
smtpclient.Host = "ldnmail";
MailMessage message = new MailMessage("nick.gowdy@orcinternational.co.uk", "nick.gowdy@orcinternational.co.uk");
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
attach.ContentDisposition.FileName = error;
message.Attachments.Add(attach);
smtpclient.Send(message);
}
#endregion
#区域CSV记录
public static void initiateCsvLogging(SortedList logList,SortedList errorList)//处理csv上载日志
{
logMsgError(错误列表,日志列表);
}
公共静态无效日志错误(分类列表错误列表、分类列表日志列表)
{
使用(MemoryStream ms=new MemoryStream())
{
StreamWriter sw=新StreamWriter(毫秒);
尝试
{
sw.WriteLine(“错误:”);
foreach(错误列表中的KeyValuePair k)
{
如果(k.Value==null)
{
sw.WriteLine(“未报告错误”);
}
其他的
{
sw.WriteLine(k.Key+“,”+k.Value);
}
}
}
捕获(例外情况除外)
{
}
最后
{
西南书写线(“”);
}
logMsg(logList,ms);
}
}//处理csv的数据上载错误日志记录
公共静态MemoryStream logMsg(SortedList logList,MemoryStream ms)
{
字符串DateNow=System.DateTime.Now.Date.ToString(“yyyy-MM-dd”);
字符串FileName=“log”+DateNow+“.csv”;
char[]delimiterChars={',};
尝试
{
//将值写入文本文件并保存到日志文件夹
使用(StreamWriter sw=新StreamWriter(ms))
{
if(logList.Keys.Count==0)
{
WriteLine(“没有新用户添加到Active Directory。”);
}
否则//如果键>0,则新用户已添加到AD
{
sw.WriteLine(“用户名“+”、“+”密码“+”、“+”公司“+”、“+”电子邮件”);
foreach(日志列表中的KeyValuePair k)
{
string[]values=k.Value.Split(delimiterChars);
sw.WriteLine(值[0]+”、“+值[1]+”、“+值[2]+”、“+值[3]);
}
}
尝试
{
sw.Flush();
sendLogByEmail(新内存流(ms.ToArray()),文件名);
}
捕获(例外情况除外)
{
}
}
}
捕获(线程异常异常)
{
}
最后
{
}
返回ms;
}//处理csv的日志记录
公共静态无效sendLogByEmail(MemoryStream ms、字符串错误、整数计数)
{
//通过附件代码发送邮件
SmtpClient SmtpClient=新的SmtpClient();
//smtpclient.DeliveryMethod=SmtpDeliveryMethod.PickupDirectoryFromIis;
//smtpclient.UseDefaultCredentials=true;
smtpclient.Host=“ldnmail”;
MailMessage=新的MailMessage(“尼克。gowdy@orcinternational.co.uk“尼克。gowdy@orcinternational.co.uk");
System.Net.Mime.ContentType ct=新的System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment attach=新的System.Net.Mail.Attachment(ms,ct);
attach.ContentDisposition.FileName=错误;
message.Attachments.Add(附加);
发送(消息);
}
#端区
因为我没有使用using关键字,所以我必须为memorystream对象编写更多的代码吗?您必须刷新(关闭)StreamWriter
finally
{
sw.WriteLine("");
}
sw.Flush();
logMsg(logList, ms);
但最终您使用所有数据通过邮件发送:
System.Net.Mime.ContentType ct = new
System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
通过附加的StringWriter在StringBuilder中收集信息似乎要容易得多
StreamWriter关闭其流时会出现问题,这可能是您的问题的一部分。您能否清楚地说明
内存流在哪一点是空的,这与您的预期相反?还有:空的catch
会立即让我产生怀疑。。。你能把它们注释掉吗,看看是否有异常发生?如果我写它们,然后尝试读取而不首先将其位置重置为0,我偶尔会遇到“empty”MemoryStream
s。我不知道这是否是您的问题,但值得检查。@MarcGravel我在空的catch块中放入了一个ex.tostring(),代码中没有异常。不包括冲洗是问题所在。@HankHolterman感谢这起作用。我阅读了flush方法的描述,它说导致缓冲数据写入流。所以我猜数据以前没有写入内存流?