I am building an web app and use Log4Mongo to store the app logs into "Logs" collection in Mongo database.
Here is the MongoDBAppender in web.config
<appender name="MongoDBAppender" type="Log4Mongo.MongoDBAppender, Log4Mongo">
<connectionString value="mongodb://localhost/insurancegrader?safe=true" />
<collectionName value="Logs" />
<field>
<name value="timestamp" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</field>
<field>
<name value="level" />
<layout type="log4net.Layout.PatternLayout" value="%level" />
</field>
<field>
<name value="thread" />
<layout type="log4net.Layout.PatternLayout" value="%thread" />
</field>
<field>
<name value="logger" />
<layout type="log4net.Layout.PatternLayout" value="%logger" />
</field>
<field>
<name value="message" />
<layout type="log4net.Layout.PatternLayout" value="%message" />
</field>
<field>
<name value="mycustomproperty" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="mycustomproperty" />
</layout>
</field>
</appender>
JSON format like this
{ "_id" : ObjectId("51921692a777da8788faa22c"), "timestamp" : ISODate("2013-05-14T10:48:50.018Z"), "level" : "ERROR", "thread" : "100", "logger" : "type of class", "message" : "some info here" }
I build a class to store the object after deserialization
public class LogEntry
{
public ObjectId Id { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
[JsonProperty("timestamp")]
public DateTime TimeStamp { get; set; }
[JsonProperty("level")]
public string Level { get; set; }
[JsonProperty("logger")]
public string Logger { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
}
However, I get an error when deserialize the logs JSON to LogEntry object but not sure why. Any help or advice is much appreciated!
Element 'timestamp' does not match any field or property of class
The simple resolution is to correct all properties of LogEntry class to the same as they are in JSON format (remove JsonProperty(...), but it is leading to convention code issue)
public class LogEntry {
public ObjectId Id { get; set; }
public DateTime timestamp{ get; set; }
public string level{ get; set; }
public string logger{ get; set; }
public string message{ get; set; }
}