diff --git a/NLog.Target.Gelf/GelfMessage.cs b/NLog.Target.Gelf/GelfMessage.cs index 9c990b8..0973beb 100644 --- a/NLog.Target.Gelf/GelfMessage.cs +++ b/NLog.Target.Gelf/GelfMessage.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using Newtonsoft.Json; namespace NLog.Targets.Gelf @@ -34,5 +35,8 @@ internal class GelfMessage [JsonProperty("_notes")] public string Notes { get; set; } + + [JsonExtensionData] + public Dictionary Properties { get; set; } } } \ No newline at end of file diff --git a/NLog.Target.Gelf/NLog.Targets.Gelf.cs b/NLog.Target.Gelf/NLog.Targets.Gelf.cs index dcb28ed..734ee65 100644 --- a/NLog.Target.Gelf/NLog.Targets.Gelf.cs +++ b/NLog.Target.Gelf/NLog.Targets.Gelf.cs @@ -142,7 +142,8 @@ private string CreateGelfJsonFromLoggingEvent(LogEventInfo logEventInfo) Host = Dns.GetHostName(), Level = logEventInfo.Level.GelfSeverity(), ShortMessage = shortMessage, - Logger = logEventInfo.LoggerName ?? "" + Logger = logEventInfo.LoggerName ?? "", + Properties = logEventInfo.Properties.ToDictionary(x => "_" + x.Key, y => y.Value) }; if (logEventInfo.Properties != null) diff --git a/TestApp/Form1.Designer.cs b/TestApp/Form1.Designer.cs index 9dc78bd..b6be99b 100644 --- a/TestApp/Form1.Designer.cs +++ b/TestApp/Form1.Designer.cs @@ -39,6 +39,7 @@ private void InitializeComponent() this.lblRandomMessageSize = new System.Windows.Forms.Label(); this.buttonCustomMessage = new System.Windows.Forms.Button(); this.tbxCustomMessage = new System.Windows.Forms.TextBox(); + this.buttonCustomFields = new System.Windows.Forms.Button(); this.SuspendLayout(); // // buttonTrace @@ -103,7 +104,7 @@ private void InitializeComponent() // // buttonRandomMessage // - this.buttonRandomMessage.Location = new System.Drawing.Point(34, 290); + this.buttonRandomMessage.Location = new System.Drawing.Point(34, 334); this.buttonRandomMessage.Name = "buttonRandomMessage"; this.buttonRandomMessage.Size = new System.Drawing.Size(212, 38); this.buttonRandomMessage.TabIndex = 6; @@ -113,7 +114,7 @@ private void InitializeComponent() // // tbxRandomMessageSize // - this.tbxRandomMessageSize.Location = new System.Drawing.Point(153, 334); + this.tbxRandomMessageSize.Location = new System.Drawing.Point(153, 378); this.tbxRandomMessageSize.MaxLength = 7; this.tbxRandomMessageSize.Name = "tbxRandomMessageSize"; this.tbxRandomMessageSize.Size = new System.Drawing.Size(93, 20); @@ -122,7 +123,7 @@ private void InitializeComponent() // lblRandomMessageSize // this.lblRandomMessageSize.AutoSize = true; - this.lblRandomMessageSize.Location = new System.Drawing.Point(31, 337); + this.lblRandomMessageSize.Location = new System.Drawing.Point(31, 381); this.lblRandomMessageSize.Name = "lblRandomMessageSize"; this.lblRandomMessageSize.Size = new System.Drawing.Size(116, 13); this.lblRandomMessageSize.TabIndex = 8; @@ -130,7 +131,7 @@ private void InitializeComponent() // // buttonCustomMessage // - this.buttonCustomMessage.Location = new System.Drawing.Point(34, 360); + this.buttonCustomMessage.Location = new System.Drawing.Point(34, 404); this.buttonCustomMessage.Name = "buttonCustomMessage"; this.buttonCustomMessage.Size = new System.Drawing.Size(212, 38); this.buttonCustomMessage.TabIndex = 9; @@ -140,17 +141,28 @@ private void InitializeComponent() // // tbxCustomMessage // - this.tbxCustomMessage.Location = new System.Drawing.Point(34, 404); + this.tbxCustomMessage.Location = new System.Drawing.Point(34, 448); this.tbxCustomMessage.Multiline = true; this.tbxCustomMessage.Name = "tbxCustomMessage"; this.tbxCustomMessage.Size = new System.Drawing.Size(212, 236); this.tbxCustomMessage.TabIndex = 10; // + // buttonCustomFields + // + this.buttonCustomFields.Location = new System.Drawing.Point(34, 290); + this.buttonCustomFields.Name = "buttonCustomFields"; + this.buttonCustomFields.Size = new System.Drawing.Size(212, 38); + this.buttonCustomFields.TabIndex = 11; + this.buttonCustomFields.Text = "Custom Fields"; + this.buttonCustomFields.UseVisualStyleBackColor = true; + this.buttonCustomFields.Click += new System.EventHandler(this.ButtonLog_Click); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(284, 652); + this.ClientSize = new System.Drawing.Size(284, 721); + this.Controls.Add(this.buttonCustomFields); this.Controls.Add(this.tbxCustomMessage); this.Controls.Add(this.buttonCustomMessage); this.Controls.Add(this.lblRandomMessageSize); @@ -186,6 +198,7 @@ private void InitializeComponent() private System.Windows.Forms.Label lblRandomMessageSize; private System.Windows.Forms.Button buttonCustomMessage; private System.Windows.Forms.TextBox tbxCustomMessage; + private System.Windows.Forms.Button buttonCustomFields; } } diff --git a/TestApp/Form1.cs b/TestApp/Form1.cs index 2f420cd..af2828f 100644 --- a/TestApp/Form1.cs +++ b/TestApp/Form1.cs @@ -48,6 +48,9 @@ private void ButtonLog_Click(object sender, EventArgs e) case "buttonFatal": Logger.Fatal("This is a sample fatal message"); break; + case "buttonCustomFields": + LogEventWithCustomProperties(LogLevel.Trace, "This is a message with custom fields"); + break; case "buttonRandomMessage": // 32 is a space, the rest are the upper case alphabet. var byteValues = new[] {32, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90}; @@ -65,5 +68,13 @@ private void ButtonLog_Click(object sender, EventArgs e) break; } } + + private static void LogEventWithCustomProperties(LogLevel level, string message) + { + var theEvent = new LogEventInfo(level, "logger-name", message); + theEvent.Properties["MyCustomValue"] = "custom"; + theEvent.Properties["AnotherCustomValue"] = "custom2"; + Logger.Log(theEvent); + } } }