129 lines
4.7 KiB
C#
129 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using com.itac.mes.tools;
|
|
|
|
namespace com.itac.mes.tools
|
|
{
|
|
public class MessageWriter
|
|
{
|
|
static int counter = 0;
|
|
bool xmlExport = false;
|
|
bool xmlLogging = false;
|
|
private HashSet<String> writeXmlForType;
|
|
|
|
/// <summary>
|
|
/// Writing the serialized content of the mesage to a file.
|
|
/// Before an instance is written its simple type name must be added to the writer <see cref="AddType"/>
|
|
/// If xmlExport is false no message will be serialized.
|
|
/// If xml Logging is false only the simple type name is printed to console.
|
|
/// </summary>
|
|
/// <param name="xmlExport"></param>
|
|
/// <param name="xmlLogging"></param>
|
|
public void init(bool xmlExport, bool xmlLogging)
|
|
{
|
|
try
|
|
{
|
|
this.xmlExport = xmlExport;
|
|
this.xmlLogging = xmlLogging;
|
|
|
|
// pruefen, dass das Ausgabeverzeichnis ./output existiert
|
|
bool exists = System.IO.Directory.Exists(@".\output");
|
|
if (!exists && xmlExport)
|
|
{
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Critical, "xmlExport set to true, but output folder (./output) does not exist!");
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Information, "flag xmlExport modified, is set to false now!");
|
|
xmlExport = false;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Critical, "Initialization failed! " + e.Message, e);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public void output(Object outputObject)
|
|
{
|
|
if (outputObject == null)
|
|
{
|
|
return;
|
|
}
|
|
DateTime starttime = DateTime.Now;
|
|
counter++;
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Verbose, "start serializing message");
|
|
String typeName = outputObject.GetType().Name;
|
|
// bei Array-Typen die Klammern im Typnamen weglassen
|
|
if (typeName.EndsWith("[]"))
|
|
{
|
|
typeName = typeName.Substring(0, typeName.Length - 2);
|
|
}
|
|
String XmlizedString = null;
|
|
if (xmlLogging)
|
|
{
|
|
ItacXmlSerializer itacSerializer = new ItacXmlSerializer();
|
|
try
|
|
{
|
|
XmlizedString = itacSerializer.serialize(outputObject).ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex);
|
|
XmlizedString = "serializing data for " + typeName + " failed!";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
XmlizedString = "<" + typeName + ">...";
|
|
}
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Verbose, XmlizedString);
|
|
if (xmlExport)
|
|
{
|
|
try
|
|
{
|
|
String fileName = counter + "_" + DateTime.Now.Ticks + "_" + typeName + ".xml";
|
|
string newPath = Path.Combine(@".\output", fileName);
|
|
bool writeFile = getWriteXmlForType().Contains(typeName);
|
|
if (writeFile && counter < 65535)
|
|
{
|
|
Stream fileStream = new FileStream(newPath, FileMode.Create, FileAccess.Write);
|
|
using (StreamWriter outfile = new StreamWriter(fileStream))
|
|
{
|
|
outfile.Write(XmlizedString);
|
|
outfile.Flush();
|
|
outfile.Close();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Error, "writing xml file failed for {0}" + typeName, e);
|
|
}
|
|
}
|
|
TimeSpan duration = DateTime.Now - starttime;
|
|
LogHandler.log(Constants.LOGGER, TraceEventType.Verbose, "serializing message done in " + duration.Milliseconds + " millies");
|
|
}
|
|
|
|
private HashSet<string> getWriteXmlForType()
|
|
{
|
|
if (writeXmlForType == null)
|
|
{
|
|
writeXmlForType = new HashSet<String>();
|
|
}
|
|
return writeXmlForType;
|
|
}
|
|
|
|
public void AddType(string typeName)
|
|
{
|
|
if (writeXmlForType == null)
|
|
{
|
|
writeXmlForType = new HashSet<String>();
|
|
}
|
|
writeXmlForType.Add(typeName);
|
|
}
|
|
}
|
|
}
|