Ejemplo n.º 1
0
 protected override void OnBatch(MonitorEventEntry[] events)
 {
     using (CsvWriter writer = BuildWriter())
     {
         foreach (MonitorEventEntry e in events)
         {
             writer.AppendValue(e.TimeStamp);
             writer.AppendValue(e.OpCode);
             writer.AppendValue(e.Level);
             writer.AppendValue(e.EntityType);
             writer.AppendValue(e.EntityIdentity);
             writer.AppendValue(e.Name);
             writer.AppendValue(e.User);
             writer.AppendValue(e.Detail);
             writer.AppendValue(e.Source);
             writer.AppendValue(e.RuntimeMilliseconds);
             writer.AppendRowSeparator();
         }
     }
 }
Ejemplo n.º 2
0
        private static IResponse ToCsvResponse(SelectResult result, string fileName)
        {
            const string outputMimeType = "text/csv; encoding=utf-8";

            var resp = new StreamWriterResponse(outputMimeType, async(s) =>
            {
                SerializationContext context = new SerializationContext(s);
                var items = result.Values;

                // ***Crazy Excel Business***
                // This is pretty ugly. If the first 2 chars in a CSV file as ID, then excel is  thinks the file is a SYLK
                // file not a CSV File (!) and will alert the user. Excel does not care about output mime types.
                //
                // To work around this, and have a _nice_ experience for csv export, we'll modify
                // the first column name to " ID" to trick Excel. It's not perfect, but it'll do.
                //
                // As a mitigation for round-tripping, the CsvReader will trim column names. Sigh.
                List <string> columns = new List <string>();

                foreach (ColumnDetails column in items.Columns)
                {
                    if (columns.Count == 0 && column.Name.Equals("ID", StringComparison.OrdinalIgnoreCase))
                    {
                        columns.Add(" ID");
                    }
                    else
                    {
                        columns.Add(column.Name);
                    }
                }

                CsvWriter writer = new CsvWriter(context, columns);

                for (int row = 0; row < items.RowCount; ++row)
                {
                    for (int col = 0; col < items.ColumnCount; ++col)
                    {
                        writer.AppendValue(items[row, col]);
                    }

                    writer.AppendRowSeparator();
                }

                context.Writer.Flush();
                await s.FlushAsync();
            });

            resp.AddHeader("Content-Disposition", String.Concat("attachment;filename=\"", fileName, "\";"));

            return(resp);
        }