我有日志我试图编写一个实用程序来解析。
这些日志的结构为CSV,但部分(列)的数量取决于"交易类型"。约有50种不同的可能性"交易类型"所以为每个人上课是不可行的。
示例日志条目:
2016-08-02 10:36:30.578 (Machine: 00124) Received: 1120228,T,2016-08-02 10:36:30,5,18441,209719,1,20
2016-08-02 10:36:30.594 (Machine: 00124) Sent: 1120228,T,2016-08-02 10:36:30,0,,0,18441,DhfMAAfjhsb6CiW55999999TbNakCC3Fc8jaZ
每个Received
都有匹配的Sent
。值和表示的参数因Received
和Sent
以及交易类型而异。
这个想法和当前的实施情况如下。
Received
或Sent
当前实施:
public class Message : IMessage
{
private List<Received> _receivedMessage;
private List<Sent> _sentMessage;
public Message()
{
ReceivedMessage = new List<Received>();
SentMessage = new List<Sent>();
}
public Message(
string messageID,
List<Received> receivedMessage,
List<Sent> sentMessage,
string storedProcedure,
string transShortName)
{
MessageID = messageID;
ReceivedMessage = receivedMessage;
SentMessage = sentMessage;
StoredProcedure = storedProcedure;
TransShortName = transShortName;
}
public string MessageID { get; set; }
public List<Received> ReceivedMessage
{
get { return new List<Received>(_receivedMessage.OrderBy(_ => _.Position)); }
set { _receivedMessage = value; }
}
public List<Sent> SentMessage
{
get { return new List<Sent>(_sentMessage.OrderBy(_ => _.Position)); }
set { _sentMessage = value; }
}
public string StoredProcedure { get; set; }
public string TransShortName { get; set; }
public class Sent : ISent
{
public string MessageID { get; set; }
public dynamic Value { get; set; }
public string Parameter { get; set; }
public string Position { get; set; }
}
public class Received : IReceived
{
public string MessageID { get; set; }
public dynamic Value { get; set; }
public string Error { get; set; }
public string Parameter { get; set; }
public string Position { get; set; }
public string Regex { get; set; }
//public string TransShortName { get; set; }
}
}
ISent界面:
public interface ISent
{
string Parameter { get; set; }
string Position { get; set; }
dynamic Value { get; set; }
}
IReceived Interface:
public interface IReceived
{
string Error { get; set; }
string Parameter { get; set; }
string Position { get; set; }
string Regex { get; set; }
//string TransShortName { get; set; }
}
理念,最佳实践,任何事情都受到欢迎!
答案 0 :(得分:2)
如果您不希望每种类型都有一个类,那么我建议将常用属性作为常规属性,将动态属性作为键/值对添加到字典中。在您的代码中的某个时刻,您将不得不知道某种类型的属性。
此外,如果您拥有所有50种类型的模式,则可以使用XSD命令行工具从模式生成C#类。命名约定可能有点古怪:)。