Reputation: 301
as the title says, I'm trying to serialize a List into a byte[] to be able to send it through a zeromq socket.
First of all, what I'm trying to achieve is: get all the errors (from certain sources) from the Windows event log. Then, throw this inside an object, which has a list, and then serialize it and send it through a zeromq socket.
This are two classes I use to store my information
public class EventErrorList
{
public int transmiter { get; set; }
public List<EventEntry> events { get; set; }
}
public class EventEntry
{
public string msg { get; set; }
public string source { get; set; }
}
And this is the code where I process the errors:
public static void Object(ref NetMQSocket response, string msgResponseCommand)
{
try
{
EventLog myLog = new EventLog();
myLog.Log = "Application";
myLog.Source = "Application Error";
var eventError = new EventErrorList();
var listError = new List<EventEntry>();
eventError.transmiter = ManagerDb.GetMachineId();
for (int index = myLog.Entries.Count - 1; index > 0; index--)
{
var errLastEntry = myLog.Entries[index];
if (errLastEntry.EntryType == EventLogEntryType.Error)
{
var appName = errLastEntry;
if (errLastEntry.Source == ".NET Runtime" || errLastEntry.Source == "MSSQLSERVER" || errLastEntry.Source == "Service1")
{
var entry = new EventEntry();
entry.msg = appName.Message;
entry.source = appName.Source;
listError.Add(entry);
}
}
}
eventError.events = listError;
var binFormatter = new BinaryFormatter();
var mStream = new MemoryStream();
binFormatter.Serialize(mStream, eventError);
response.SendFrame(mStream.ToArray());
}
catch (Exception ex)
{
Console.WriteLine("Error");
Console.WriteLine(ex.Message);
}
}
Up until eventError.events = listError;
, the code works great. But, from there, it is not working properly. I got this method of serialization from another thread in SO, but in the line binFormatter.Serialize(mStream, eventError);
it is triggering the try/catch, and jumping into the exception.
This is the exception I'm getting:
Exception thrown: 'System.Runtime.Serialization.SerializationException' in mscorlib.dll
I'm sorry if this is an obvious question, but I don't really code in c#, and I'm stuck with adding this functionality to an already existing code.
Upvotes: 0
Views: 135
Reputation: 1662
Adding a note here that since .NET 5.0, binary serialization is deprecated due to potential security vulnerabilities. See https://aka.ms/binaryformatter for details.
As a workaround, use System.Text.Json.JsonSerializer
or an XML serializer.
Upvotes: 0
Reputation: 250366
You need to mark the classes with the Serializable
attribute:
[Serializable]
public class EventErrorList
{
public int transmiter { get; set; }
public List<EventEntry> events { get; set; }
}
[Serializable]
public class EventEntry
{
public string msg { get; set; }
public string source { get; set; }
}
For future reference you should read and post the error message as well:
'Type 'q48506847.EventErrorList' in Assembly 'q48506847, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.'
Upvotes: 1