Skip to content

Commit

Permalink
Merge pull request #15 from MarkZither/Issue14_ProcId
Browse files Browse the repository at this point in the history
Fix issue 14 + allow specify ProcId in config
  • Loading branch information
MarkZither authored Dec 14, 2021
2 parents 5dc8fb4 + 00e64a3 commit c08219a
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# top-most EditorConfig file
root = true
indent_style = space

[*.{cs,vb}]
indent_style=space
tab_width=4
indent_size=4
13 changes: 12 additions & 1 deletion src/Log4net.Appender.InfluxDBSyslog/BufferingInfluxAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using log4net.Util.TypeConverters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -55,6 +56,7 @@ public int RemotePort
private int _remotePort;
public Facility Facility { get; set; }
public AppName AppName { get; set; }
public ProcId ProcId { get; set; }


private readonly HttpClient HttpClient;
Expand All @@ -63,6 +65,7 @@ public BufferingInfluxAppender()
{
ConverterRegistry.AddConverter(typeof(AppName), new ConvertStringToAppName());
ConverterRegistry.AddConverter(typeof(Facility), new ConvertStringToFacility());
ConverterRegistry.AddConverter(typeof(ProcId), new ConvertStringToProcId());
//https://github.com/dotnet/extensions/issues/1345
HttpClient = new HttpClient();
}
Expand Down Expand Up @@ -128,12 +131,19 @@ protected override void SendBuffer(LoggingEvent[] events)

foreach (var loggingEvent in events)
{
string procId = $"{Process.GetCurrentProcess().Id}|{Process.GetCurrentProcess().ProcessName}";
if (ProcId != null && !string.IsNullOrWhiteSpace(ProcId.Value))
{
ProcId.FormatValue(loggingEvent);
procId = ProcId.Value;
}

SyslogSeverity severity = Log4netSyslogSeverityConvertor.GetSyslogSeverity(loggingEvent.Level);

var fields = new Dictionary<string, object>();
fields.Add("facility_code", 16);
fields.Add("message", loggingEvent.MessageObject);
fields.Add("procid", "1234");
fields.Add("procid", procId);
fields.Add("severity_code", severity.SeverityCode);
fields.Add("timestamp", UnixTimestampFromDateTime(loggingEvent.TimeStamp));
fields.Add("version", 1);
Expand Down Expand Up @@ -161,6 +171,7 @@ protected override void SendBuffer(LoggingEvent[] events)
base.ErrorHandler.Error($"{nameof(InfluxAppender)} Emit - {ex.Message}");
}
}

public static long UnixTimestampFromDateTime(DateTime date)
{
long unixTimestamp = date.Ticks - new DateTime(1970, 1, 1).Ticks;
Expand Down
12 changes: 11 additions & 1 deletion src/Log4net.Appender.InfluxDBSyslog/InfluxAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using log4net.Util.TypeConverters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -54,6 +55,7 @@ public int RemotePort
private int _remotePort;
public Facility Facility { get; set; }
public AppName AppName { get; set; }
public ProcId ProcId { get; set; }


private readonly HttpClient HttpClient;
Expand All @@ -62,6 +64,7 @@ public InfluxAppender()
{
ConverterRegistry.AddConverter(typeof(AppName), new ConvertStringToAppName());
ConverterRegistry.AddConverter(typeof(Facility), new ConvertStringToFacility());
ConverterRegistry.AddConverter(typeof(ProcId), new ConvertStringToProcId());
//https://github.com/dotnet/extensions/issues/1345
HttpClient = new HttpClient();
}
Expand All @@ -83,10 +86,17 @@ protected override async void Append(LoggingEvent loggingEvent)
HttpClient);
InfluxData.Net.InfluxDb.InfluxDbClient client = new InfluxData.Net.InfluxDb.InfluxDbClient(config);

string procId = $"{Process.GetCurrentProcess().Id}|{Process.GetCurrentProcess().ProcessName}";
if(ProcId != null && !string.IsNullOrWhiteSpace(ProcId.Value))
{
ProcId.FormatValue(loggingEvent);
procId = ProcId.Value;
}

var fields = new Dictionary<string, object>();
fields.Add("facility_code", 16);
fields.Add("message", loggingEvent.MessageObject);
fields.Add("procid", "1234");
fields.Add("procid", procId);
fields.Add("severity_code", severity.SeverityCode);
fields.Add("timestamp", DateTimeOffset.Now.ToUnixTimeMilliseconds() * 1000000);
fields.Add("version", 1);
Expand Down
133 changes: 133 additions & 0 deletions src/Log4net.Appender.InfluxDBSyslog/ProcId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using log4net.Core;
using log4net.Layout;
using log4net.Util.TypeConverters;
using System;

namespace Log4net.Appender.InfluxDBSyslog
{
/// <summary>
/// Parameter type used by the <see cref="ProcId"/>.
/// </summary>
/// <remarks>
/// <para>
/// This class provides the basic database parameter properties
/// as defined by the <see cref="System.Data.IDbDataParameter"/> interface.
/// </para>
/// <para>This type can be subclassed to provide database specific
/// functionality. The two methods that are called externally are
/// <see cref="Prepare"/> and <see cref="FormatValue"/>.
/// </para>
/// </remarks>
public class ProcId
{
#region Public Instance Constructors

/// <summary>
/// Initializes a new instance of the <see cref="ProcId" /> class.
/// </summary>
/// <remarks>
/// Default constructor for the ProcId class.
/// </remarks>
public ProcId()
{

}

public ProcId(string value)
{
Value = value;
}

#endregion Public Instance Constructors

#region Public Instance Properties

/// <summary>
/// Gets or sets the name of this parameter.
/// </summary>
/// <value>
/// The name of this parameter.
/// </value>
/// <remarks>
/// <para>
/// The name of this parameter. The parameter name
/// must match up to a named parameter to the SQL stored procedure
/// or prepared statement.
/// </para>
/// </remarks>
public string Value { get; set; }

/// <summary>
/// Gets or sets the <see cref="IRawLayout"/> to use to
/// render the logging event into an object for this
/// parameter.
/// </summary>
/// <value>
/// The <see cref="IRawLayout"/> used to render the
/// logging event into an object for this parameter.
/// </value>
/// <remarks>
/// <para>
/// The <see cref="IRawLayout"/> that renders the value for this
/// parameter.
/// </para>
/// <para>
/// The <see cref="RawLayoutConverter"/> can be used to adapt
/// any <see cref="ILayout"/> into a <see cref="IRawLayout"/>
/// for use in the property.
/// </para>
/// </remarks>
public IRawLayout Layout { get; set; }

#endregion Public Instance Properties

#region Public Instance Methods

/// <summary>
/// Renders the logging event and set the parameter value in the command.
/// </summary>
/// <param name="loggingEvent">The event to be rendered.</param>
/// <remarks>
/// <para>
/// Renders the logging event using this parameters layout
/// object. Sets the value of the parameter on the command object.
/// </para>
/// </remarks>
virtual public void FormatValue(LoggingEvent loggingEvent)
{
if (Layout is IRawLayout)
{
// Format the value
Value = Layout.Format(loggingEvent) as string;
}
}

public override string ToString()
{
return Value;
}

#endregion Public Instance Methods
#region Private Instance Fields

#endregion Private Instance Fields
}

public class ConvertStringToProcId : IConvertFrom
{
bool IConvertFrom.CanConvertFrom(Type sourceType)
{
return sourceType == typeof(string);
}

object IConvertFrom.ConvertFrom(object source)
{
string str = source as string;
if (str != null)
{
return new ProcId(str);
}
throw ConversionNotSupportedException.Create(typeof(ProcId), source);
}
}
}

0 comments on commit c08219a

Please sign in to comment.