{timestamp} EventId={...} EventName={...} Level={...} "FormattedMessage={...}"
26 | /// {timestamp} EventId={...} EventName={...} Level={...} "FormattedMessage={...}").
47 | public UdpEventSink(IPAddress host, int port, IEventTextFormatter formatter = null) :
48 | this(Util.OpenUdpSocket(host, port), formatter) {}
49 |
50 | /// {timestamp} EventId={...} EventName={...} Level={...} "FormattedMessage={...}").
57 | public UdpEventSink(string host, int port, IEventTextFormatter formatter = null) :
58 | this(host.HostnameToIPAddress(), port, formatter) { }
59 |
60 | public void OnCompleted()
61 | {
62 | socket.Close();
63 | socket.Dispose();
64 | }
65 |
66 | public void OnError(Exception error)
67 | {
68 | socket.Close();
69 | socket.Dispose();
70 | }
71 |
72 | public void OnNext(EventEntry value)
73 | {
74 | var sw = new StringWriter();
75 | formatter.WriteEvent(value, sw);
76 | socket.Send(Encoding.UTF8.GetBytes(sw.ToString()));
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/standalone-test/standalone-test.csproj:
--------------------------------------------------------------------------------
1 |
2 | {timestamp} EventId={...} EventName={...} Level={...} "FormattedMessage={...}").
44 | /// An object defining the reconnect policy in the event
45 | /// of TCP errors (default: an ExponentialBackoffTcpConnectionPolicy object).
46 | /// The maximum number of events to queue in the event of
47 | /// the TCP session dropping before events start to be dropped (defaults to 10,000).
48 | /// A progress reporter that triggers when events are written from
49 | /// the queue to the TCP port (defaults to a new Progress object). It is reachable
50 | /// via the Progress property.
51 | public TcpEventSink(IPAddress host, int port, ITcpReconnectionPolicy policy,
52 | IEventTextFormatter formatter = null, int maxQueueSize = 10000)
53 | {
54 | this.writer = new TcpSocketWriter(host, port, policy, maxQueueSize);
55 | this.formatter = formatter;
56 | }
57 |
58 | /// {timestamp} EventId={...} EventName={...} Level={...} "FormattedMessage={...}").
76 | /// An object defining the reconnect policy in the event
77 | /// of TCP errors (default: an ExponentialBackoffTcpConnectionPolicy object).
78 | /// The maximum number of events to queue in the event of
79 | /// the TCP session dropping before events start to be dropped (defaults to 10,000).
80 | /// A progress reporter that triggers when events are written from
81 | /// the queue to the TCP port (defaults to a new Progress object). It is reachable
82 | /// via the Progress property.
83 | public TcpEventSink(string host, int port, ITcpReconnectionPolicy policy,
84 | IEventTextFormatter formatter = null, int maxQueueSize = 10000) :
85 | this(host.HostnameToIPAddress(), port, policy, formatter, maxQueueSize) { }
86 |
87 | public void OnCompleted()
88 | {
89 | this.writer.Dispose();
90 | }
91 |
92 | public void OnError(Exception error)
93 | {
94 | this.writer.Dispose();
95 | }
96 |
97 | public void OnNext(EventEntry value)
98 | {
99 | var sw = new StringWriter();
100 | formatter.WriteEvent(value, sw);
101 | this.writer.Enqueue(sw.ToString());
102 | }
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/src/Splunk.Logging.Common/HttpEventCollectorResendMiddleware.cs:
--------------------------------------------------------------------------------
1 | /**
2 | * @copyright
3 | *
4 | * Copyright 2013-2015 Splunk, Inc.
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License"): you may
7 | * not use this file except in compliance with the License. You may obtain
8 | * a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 | * License for the specific language governing permissions and limitations
16 | * under the License.
17 | */
18 |
19 | using System;
20 | using System.Collections.Generic;
21 | using System.Net;
22 | using System.Net.Http;
23 | using System.Threading.Tasks;
24 |
25 | namespace Splunk.Logging
26 | {
27 | ///
32 | /// trace.listeners.Add(new HttpEventCollectorTraceListener(
33 | /// uri: new Uri("https://localhost:8088"),
34 | /// token: "E6099437-3E1F-4793-90AB-0E5D9438A918",
35 | /// new HttpEventCollectorResendMiddleware(10).Plugin // retry 10 times
36 | /// );
37 | ///
38 | ///
30 | /// var listener = new ObservableEventListener();
31 | /// var sink = new HttpEventCollectorEventSink(
32 | /// uri: new Uri("https://localhost:8088"),
33 | /// token: "E6099437-3E1F-4793-90AB-0E5D9438A918",
34 | /// formatter: new AppEventFormatter()
35 | /// );
36 | /// listener.Subscribe(sink);
37 | /// var eventSource = new AppEventSource();
38 | /// listener.EnableEvents(eventSource, EventLevel.LogAlways, Keywords.All);
39 | /// eventSource.Message("Hello world");
40 | ///
41 | /// AppEventFormatter and AppEventSource have to be implemented by user. See
42 | /// TestHttpEventCollector.cs for a working example.
43 | ///
44 | /// Trace sink supports events batching (off by default) that allows to
45 | /// decrease number of HTTP requests to Splunk server. The batching is
46 | /// controlled by three parameters: "batch size count", "batch size bytes"
47 | /// and "batch interval". If batch size parameters are specified then
48 | /// Send(...) multiple events are sending simultaneously batch exceeds its limits.
49 | /// Batch interval controls a timer that forcefully sends events batch
50 | /// regardless of its size.
51 | ///
52 | /// var listener = new ObservableEventListener();
53 | /// var sink = new HttpEventCollectorEventSink(
54 | /// uri: new Uri("https://localhost:8088"),
55 | /// token: "E6099437-3E1F-4793-90AB-0E5D9438A918",
56 | /// formatter: new AppEventFormatter(),
57 | /// batchInterval: 1000, // send events at least every second
58 | /// batchSizeBytes: 1024, // 1KB
59 | /// batchSizeCount: 10) // events batch contains at most 10 individual events
60 | /// );
61 | /// listener.Subscribe(sink);
62 | /// var eventSource = new AppEventSource();
63 | /// listener.EnableEvents(eventSource, EventLevel.LogAlways, Keywords.All);
64 | /// eventSource.Message("Hello batching 1");
65 | /// eventSource.Message("Hello batching 2");
66 | /// eventSource.Message("Hello batching 3");
67 | ///
68 | ///
69 | /// To improve system performance tracing events are sent asynchronously and
70 | /// events with the same timestamp (that has 1 millisecond resolution) may
71 | /// be indexed out of order by Splunk. sendMode parameter triggers
72 | /// "sequential mode" that guarantees preserving events order. In
73 | /// "sequential mode" performance of sending events to the server is lower.
74 | ///
75 | /// There is an ability to plug middleware components that act before and
76 | /// after posting data.
77 | /// For example:
78 | ///
79 | /// var sink = new HttpEventCollectorEventSink(
80 | /// uri: new Uri("https://localhost:8088"),
81 | /// token: "E6099437-3E1F-4793-90AB-0E5D9438A918",
82 | /// formatter: new AppEventFormatter(),
83 | /// middleware: (request, next) => {
84 | /// // preprocess request
85 | /// var response = next(request); // post data
86 | /// // process response
87 | /// return response;
88 | /// }
89 | /// ...
90 | /// )
91 | ///
92 | /// Middleware components can apply additional logic before and after posting
93 | /// the data to Splunk server. See HttpEventCollectorResendMiddleware.
94 | ///
95 | ///
96 | /// A user application code can register an error handler that is invoked
97 | /// when HTTP event collector isn't able to send data.
98 | ///
99 | /// var sink = new HttpEventCollectorEventSink(
100 | /// uri: new Uri("https://localhost:8088"),
101 | /// token: "E6099437-3E1F-4793-90AB-0E5D9438A918",
102 | /// formatter: new AppEventFormatter(),
103 | /// );
104 | /// sink.AddLoggingFailureHandler((sender, HttpEventCollectorException e) =>
105 | /// {
106 | /// // do something
107 | /// });
108 | ///
109 | /// HttpEventCollectorException contains information about the error and the list of
110 | /// events caused the problem.
111 | ///
32 | /// var trace = new TraceSource("logger");
33 | /// trace.listeners.Add(new HttpEventCollectorTraceListener(
34 | /// uri: new Uri("https://localhost:8088"),
35 | /// token: "E6099437-3E1F-4793-90AB-0E5D9438A918"));
36 | /// trace.TraceEvent(TraceEventType.Information, 1, "hello world");
37 | ///
38 | ///
39 | /// Trace listener supports events batching (off by default) that allows to
40 | /// decrease number of HTTP requests to Splunk server. The batching is
41 | /// controlled by three parameters: "batch size count", "batch size bytes"
42 | /// and "batch interval". If batch size parameters are specified then
43 | /// Send(...) multiple events are sending simultaneously batch exceeds its limits.
44 | /// Batch interval controls a timer that forcefully sends events batch
45 | /// regardless of its size.
46 | ///
47 | /// var trace = new TraceSource("logger");
48 | /// trace.listeners.Add(new HttpEventCollectorTraceListener(
49 | /// uri: new Uri("https://localhost:8088"),
50 | /// token: "E6099437-3E1F-4793-90AB-0E5D9438A918",
51 | /// batchInterval: 1000, // send events at least every second
52 | /// batchSizeBytes: 1024, // 1KB
53 | /// batchSizeCount: 10) // events batch contains at most 10 individual events
54 | /// );
55 | /// trace.TraceEvent(TraceEventType.Information, 1, "hello batching");
56 | ///
57 | ///
58 | /// To improve system performance tracing events are sent asynchronously and
59 | /// events with the same timestamp (that has 1 millisecond resolution) may
60 | /// be indexed out of order by Splunk. sendMode parameter triggers
61 | /// "sequential mode" that guarantees preserving events order. In
62 | /// "sequential mode" performance of sending events to the server is lower.
63 | ///
64 | /// There is an ability to plug middleware components that act before and
65 | /// after posting data.
66 | /// For example:
67 | ///
68 | /// new HttpEventCollectorTraceListener(
69 | /// uri: new Uri("https://localhost:8088"),
70 | /// token: "E6099437-3E1F-4793-90AB-0E5D9438A918,
71 | /// middleware: (request, next) => {
72 | /// // preprocess request
73 | /// var response = next(request); // post data
74 | /// // process response
75 | /// return response;
76 | /// }
77 | /// ...
78 | /// )
79 | ///
80 | /// Middleware components can apply additional logic before and after posting
81 | /// the data to Splunk server. See HttpEventCollectorResendMiddleware.
82 | ///
83 | ///
84 | /// A user application code can register an error handler that is invoked
85 | /// when HTTP event collector isn't able to send data.
86 | ///
87 | /// var listener = new HttpEventCollectorTraceListener(
88 | /// uri: new Uri("https://localhost:8088"),
89 | /// token: "E6099437-3E1F-4793-90AB-0E5D9438A918")
90 | /// );
91 | /// listener.AddLoggingFailureHandler((sender, HttpEventCollectorException e) =>
92 | /// {
93 | /// // do something
94 | /// });
95 | /// trace.listeners.Add(listener);
96 | ///
97 | /// HttpEventCollectorException contains information about the error and the list of
98 | /// events caused the problem.
99 | /// | devinfo@splunk.com | 296 ||
| Issues 300 | | 301 | https://github.com/splunk/splunk-library-dotnetlogging | 302 |
| Answers 306 | | 307 | http://splunk-base.splunk.com/tags/csharp/ | 308 |
| Blog 312 | | http://blogs.splunk.com/dev/ | 313 |
| Twitter 317 | | @splunkdev | 318 |
46 | /// new HttpEventCollectorSender(uri: ..., token: ...,
47 | /// middleware: (request, next) => {
48 | /// // preprocess request
49 | /// var response = next(request); // post data
50 | /// // process response
51 | /// return response;
52 | /// }
53 | /// ...
54 | /// )
55 | ///
56 | /// Middleware components can apply additional logic before and after posting
57 | /// the data to Splunk server. See HttpEventCollectorResendMiddleware.
58 | ///