예제 #1
0
        protected override async void Append(LoggingEvent loggingEvent)
        {
            SyslogSeverity severity = Log4netSyslogSeverityConvertor.GetSyslogSeverity(loggingEvent.Level);

            InfluxDbClientConfiguration config = new InfluxDbClientConfiguration(
                new UriBuilder(Scheme, Host, RemotePort).Uri,
                string.Empty,
                string.Empty,
                InfluxData.Net.Common.Enums.InfluxDbVersion.Latest,
                InfluxData.Net.Common.Enums.QueryLocation.FormData,
                HttpClient);

            InfluxData.Net.InfluxDb.InfluxDbClient client = new InfluxData.Net.InfluxDb.InfluxDbClient(config);

            var fields = new Dictionary <string, object>();

            fields.Add("facility_code", 16);
            fields.Add("message", loggingEvent.MessageObject);
            fields.Add("procid", "1234");
            fields.Add("severity_code", severity.SeverityCode);
            fields.Add("timestamp", DateTimeOffset.Now.ToUnixTimeMilliseconds() * 1000000);
            fields.Add("version", 1);

            var tags = new Dictionary <string, object>();

            AppName.FormatValue(loggingEvent);
            tags.Add("appname", AppName.Value);
            tags.Add("facility", Facility);
            tags.Add("host", Environment.MachineName);
            tags.Add("hostname", Environment.MachineName);
            tags.Add("severity", severity.Severity);
            try
            {
                var apiResp = await client.Client.WriteAsync(new InfluxData.Net.InfluxDb.Models.Point()
                {
                    Name      = "syslog",
                    Fields    = fields,
                    Tags      = tags,
                    Timestamp = DateTimeOffset.Now.UtcDateTime
                }, "_internal"
                                                             );

                if (!apiResp.Success)
                {
                    base.ErrorHandler.Error($"{nameof(InfluxAppender)} Write - {apiResp.Body}");
                }
            }
            catch (Exception ex)
            {
                base.ErrorHandler.Error($"{nameof(InfluxAppender)} Emit - {ex.Message}");
            }
        }
        protected override void SendBuffer(LoggingEvent[] events)
        {
            InfluxDbClientConfiguration config = new InfluxDbClientConfiguration(
                new UriBuilder(Scheme, Host, RemotePort).Uri,
                string.Empty,
                string.Empty,
                InfluxData.Net.Common.Enums.InfluxDbVersion.Latest,
                InfluxData.Net.Common.Enums.QueryLocation.FormData,
                HttpClient);

            InfluxData.Net.InfluxDb.InfluxDbClient client = new InfluxData.Net.InfluxDb.InfluxDbClient(config);

            foreach (var loggingEvent in events)
            {
                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("severity_code", severity.SeverityCode);
                fields.Add("timestamp", UnixTimestampFromDateTime(loggingEvent.TimeStamp));
                fields.Add("version", 1);

                var tags = new Dictionary <string, object>();
                AppName.FormatValue(loggingEvent);
                tags.Add("appname", AppName.Value);
                tags.Add("facility", Facility);
                tags.Add("host", Environment.MachineName);
                tags.Add("hostname", Environment.MachineName);
                tags.Add("severity", severity.Severity);
            }
            List <InfluxData.Net.InfluxDb.Models.Point> points = new List <InfluxData.Net.InfluxDb.Models.Point>();

            try
            {
                var apiResp = Task.Run(() => client.Client.WriteAsync(points, "_internal")).GetAwaiter().GetResult();

                if (!apiResp.Success)
                {
                    base.ErrorHandler.Error($"{nameof(InfluxAppender)} Write - {apiResp.Body}");
                }
            }
            catch (Exception ex)
            {
                base.ErrorHandler.Error($"{nameof(InfluxAppender)} Emit - {ex.Message}");
            }
        }