1

我正在使用 memorystream 对象将值从 streamwriter 对象写入内存。我有几种方法用于记录错误和值,因此我在它们之间传递我的 memorystream 对象作为参数。

我的问题是内存流不包含值。请参见下面的代码:

    #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

因为我没有使用 USING 关键字,所以我必须为 memorystream 对象编写更多代码吗?

4

1 回答 1

0

您必须刷新(关闭)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 关闭其 Stream 存在问题,这可能是您的问题的一部分。

于 2012-10-02T11:29:52.560 回答