diff --git a/.editorconfig b/.editorconfig index ea07552..d3c3cc8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,3 +1,8 @@ # top-most EditorConfig file root = true indent_style = space + +[*.{cs,vb}] +indent_style=space +tab_width=4 +indent_size=4 \ No newline at end of file diff --git a/src/Log4net.Appender.InfluxDBSyslog/BufferingInfluxAppender.cs b/src/Log4net.Appender.InfluxDBSyslog/BufferingInfluxAppender.cs index ee59a9a..6625a98 100644 --- a/src/Log4net.Appender.InfluxDBSyslog/BufferingInfluxAppender.cs +++ b/src/Log4net.Appender.InfluxDBSyslog/BufferingInfluxAppender.cs @@ -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; @@ -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; @@ -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(); } @@ -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(); 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); @@ -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; diff --git a/src/Log4net.Appender.InfluxDBSyslog/InfluxAppender.cs b/src/Log4net.Appender.InfluxDBSyslog/InfluxAppender.cs index 94b6c2e..a9693c7 100644 --- a/src/Log4net.Appender.InfluxDBSyslog/InfluxAppender.cs +++ b/src/Log4net.Appender.InfluxDBSyslog/InfluxAppender.cs @@ -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; @@ -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; @@ -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(); } @@ -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(); 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); diff --git a/src/Log4net.Appender.InfluxDBSyslog/ProcId.cs b/src/Log4net.Appender.InfluxDBSyslog/ProcId.cs new file mode 100644 index 0000000..e3f8148 --- /dev/null +++ b/src/Log4net.Appender.InfluxDBSyslog/ProcId.cs @@ -0,0 +1,133 @@ +using log4net.Core; +using log4net.Layout; +using log4net.Util.TypeConverters; +using System; + +namespace Log4net.Appender.InfluxDBSyslog +{ + /// + /// Parameter type used by the . + /// + /// + /// + /// This class provides the basic database parameter properties + /// as defined by the interface. + /// + /// This type can be subclassed to provide database specific + /// functionality. The two methods that are called externally are + /// and . + /// + /// + public class ProcId + { + #region Public Instance Constructors + + /// + /// Initializes a new instance of the class. + /// + /// + /// Default constructor for the ProcId class. + /// + public ProcId() + { + + } + + public ProcId(string value) + { + Value = value; + } + + #endregion Public Instance Constructors + + #region Public Instance Properties + + /// + /// Gets or sets the name of this parameter. + /// + /// + /// The name of this parameter. + /// + /// + /// + /// The name of this parameter. The parameter name + /// must match up to a named parameter to the SQL stored procedure + /// or prepared statement. + /// + /// + public string Value { get; set; } + + /// + /// Gets or sets the to use to + /// render the logging event into an object for this + /// parameter. + /// + /// + /// The used to render the + /// logging event into an object for this parameter. + /// + /// + /// + /// The that renders the value for this + /// parameter. + /// + /// + /// The can be used to adapt + /// any into a + /// for use in the property. + /// + /// + public IRawLayout Layout { get; set; } + + #endregion Public Instance Properties + + #region Public Instance Methods + + /// + /// Renders the logging event and set the parameter value in the command. + /// + /// The event to be rendered. + /// + /// + /// Renders the logging event using this parameters layout + /// object. Sets the value of the parameter on the command object. + /// + /// + 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); + } + } +} \ No newline at end of file