06 Adding Custom Data

JP Toto edited this page Feb 12, 2016 · 4 revisions

Adding custom data to your messages is easy if you log messages with an Exception class or a custom class that inherits from Exception.

If you log messages using the method signature:

_log.Error("System Error", new Exception("Something terrible happened."));

it will cause the Exception{} array to be populated in the Elasticsearch document. Since Exception has a built in Data property which is a dictionary that can be added to, you can create an Exception instance and add custom properties to the dictionary:

// Log an exception object with custom fields in the Data property
Exception newException = new Exception("There was a system error");
newException.Data.Add("CustomProperty", "CustomPropertyValue");
newException.Data.Add("SystemUserID", "User43");

_log.Error("Something broke.", newException);
	"_index": "log-2016.02.12",
	"_type": "logEvent",
	"_id": "AVLXHEwEJfnUYPcgkJ5r",
	"_version": 1,
	"_score": 1,
	"_source": {
		"timeStamp": "2016-02-12T20:11:41.5864254Z",
		"message": "Something broke.",
		"messageObject": {},
		"exception": {                                          <-- Exception Object
			"Type": "System.Exception",
			"Message": "There was a system error",
			"HelpLink": null,
			"Source": null,
			"HResult": -2146233088,
			"StackTrace": null,
			"Data": {
				"CustomProperty": "CustomPropertyValue",        <-- Custom field data
				"SystemUserID": "User43"                        <-- Custom field data
			"InnerException": null
		"loggerName": "log4net.ES.Example.Program",
		"domain": "log4net.ES.Example.vshost.exe",
		"identity": "",
		"level": "ERROR",
		"className": "log4net.ES.Example.Program",
		"fileName": "C:\\Users\\jtoto\\projects\\log4net.ES.Example\\log4net.ES.Example\\Program.cs",
		"lineNumber": "26",
		"fullInfo": "log4net.ES.Example.Program.Main(C:\\Users\\jtoto\\projects\\log4net.ES.Example\\log4net.ES.Example\\Program.cs:26)",
		"methodName": "Main",
		"fix": "LocationInfo, UserName, Identity, Partial",
		"properties": {
			"log4net:Identity": "",
			"log4net:UserName": "JToto",
			"log4net:HostName": "JToto01",
			"@timestamp": "2016-02-12T20:11:41.5864254Z"
		"userName": "JToto",
		"threadName": "9",
		"hostName": "JTOTO01"

You can see this example in action in the sample application.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.