Skip to content

Commit d65d164

Browse files
committed
Network Logger and Graylog support
1 parent 6e4701d commit d65d164

14 files changed

Lines changed: 715 additions & 18 deletions

File tree

code/src/Plexdata.LogWriter.Network.Tests/Internals/Sockets/WebClientSocketTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
using System;
3232
using System.Diagnostics.CodeAnalysis;
3333
using System.Net;
34-
using System.Threading;
3534

3635
namespace Plexdata.LogWriter.Network.Tests.Internals.Sockets
3736
{
@@ -71,6 +70,8 @@ public void Setup()
7170
this.settings.SetupGet(x => x.Host).Returns(() => this.host);
7271
this.settings.SetupGet(x => x.Port).Returns(() => this.port);
7372
this.settings.SetupGet(x => x.Timeout).Returns(() => this.timeout);
73+
this.settings.SetupGet(x => x.Method).Returns(() => this.method);
74+
this.settings.SetupGet(x => x.Content).Returns(() => this.content);
7475

7576
this.factory.Setup(x => x.Create<IClientFacade>(this.target, this.method, this.content, this.timeout)).Returns(this.client.Object);
7677
}

code/src/Plexdata.LogWriter.Network.Tests/Settings/NetworkLoggerSettingsTests.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public void NetworkLoggerSettings_ValidateDefaultSettings_DefaultSettingsAsExpec
7979
Assert.That(instance.Maximum, Is.EqualTo(8192));
8080
Assert.That(instance.Termination, Is.False);
8181
Assert.That(instance.Timeout, Is.EqualTo(100000));
82+
Assert.That(instance.Method, Is.EqualTo("POST"));
83+
Assert.That(instance.Content, Is.EqualTo("application/json"));
8284
}
8385

8486
[Test]
@@ -96,6 +98,8 @@ public void NetworkLoggerSettings_ValidateDefaultSettingsForInvalidConfiguration
9698
Assert.That(instance.Maximum, Is.EqualTo(8192));
9799
Assert.That(instance.Termination, Is.False);
98100
Assert.That(instance.Timeout, Is.EqualTo(100000));
101+
Assert.That(instance.Method, Is.EqualTo("POST"));
102+
Assert.That(instance.Content, Is.EqualTo("application/json"));
99103
}
100104

101105
[Test]
@@ -288,6 +292,33 @@ public void NetworkLoggerSettings_ConfigurationValid_GetSectionValueForTimeoutAs
288292
Assert.That(instance.Timeout, Is.EqualTo(expected));
289293
}
290294

295+
[TestCase(null, "POST")]
296+
[TestCase("", "POST")]
297+
[TestCase(" ", "POST")]
298+
[TestCase("post", "post")]
299+
[TestCase("GET", "GET")]
300+
public void NetworkLoggerSettings_ConfigurationValid_GetSectionValueForMethodAsExpected(String value, Object expected)
301+
{
302+
this.section.SetupGet(x => x["Method"]).Returns(value);
303+
304+
NetworkLoggerSettings instance = new NetworkLoggerSettings(this.configuration.Object);
305+
306+
Assert.That(instance.Method, Is.EqualTo(expected));
307+
}
308+
309+
[TestCase(null, "application/json")]
310+
[TestCase("", "application/json")]
311+
[TestCase(" ", "application/json")]
312+
[TestCase("something/else", "something/else")]
313+
public void NetworkLoggerSettings_ConfigurationValid_GetSectionValueForContentAsExpected(String value, Object expected)
314+
{
315+
this.section.SetupGet(x => x["Content"]).Returns(value);
316+
317+
NetworkLoggerSettings instance = new NetworkLoggerSettings(this.configuration.Object);
318+
319+
Assert.That(instance.Content, Is.EqualTo(expected));
320+
}
321+
291322
#endregion
292323

293324
#region Host
@@ -590,6 +621,66 @@ public void Timeout_ValueUnchanged_PropertyChangedNotFired()
590621

591622
#endregion
592623

624+
#region Method
625+
626+
[Test]
627+
public void Method_ValueChanged_PropertyChangedFired()
628+
{
629+
Boolean fired = false;
630+
NetworkLoggerSettings instance = new NetworkLoggerSettings();
631+
632+
instance.Method = "POST";
633+
instance.PropertyChanged += (sender, args) => { fired = true; };
634+
instance.Method = "GET";
635+
636+
Assert.That(fired, Is.True);
637+
}
638+
639+
[Test]
640+
public void Method_ValueUnchanged_PropertyChangedNotFired()
641+
{
642+
Boolean fired = false;
643+
NetworkLoggerSettings instance = new NetworkLoggerSettings();
644+
645+
instance.Method = "POST";
646+
instance.PropertyChanged += (sender, args) => { fired = true; };
647+
instance.Method = "POST";
648+
649+
Assert.That(fired, Is.False);
650+
}
651+
652+
#endregion
653+
654+
#region Content
655+
656+
[Test]
657+
public void Content_ValueChanged_PropertyChangedFired()
658+
{
659+
Boolean fired = false;
660+
NetworkLoggerSettings instance = new NetworkLoggerSettings();
661+
662+
instance.Content = "application/json";
663+
instance.PropertyChanged += (sender, args) => { fired = true; };
664+
instance.Content = "something/else";
665+
666+
Assert.That(fired, Is.True);
667+
}
668+
669+
[Test]
670+
public void Content_ValueUnchanged_PropertyChangedNotFired()
671+
{
672+
Boolean fired = false;
673+
NetworkLoggerSettings instance = new NetworkLoggerSettings();
674+
675+
instance.Content = "application/json";
676+
instance.PropertyChanged += (sender, args) => { fired = true; };
677+
instance.Content = "application/json";
678+
679+
Assert.That(fired, Is.False);
680+
}
681+
682+
#endregion
683+
593684
#region Other privates
594685

595686
private class EncodingTestItem

code/src/Plexdata.LogWriter.Network/Abstraction/INetworkLoggerSettings.cs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public interface INetworkLoggerSettings : ILoggerSettings
149149
/// <td><see cref="Protocol.Udp"/>, <see cref="Protocol.Tcp"/></td>
150150
/// <td><see cref="Address.IPv4"/></td><td>DNS&#160;name</td>
151151
/// <td>The <em>Host</em> property can contain a DNS host name, such as <c>localhost</c> for example,
152-
/// and will resolve to an IPv4 address. In this case the remote server must allow access over IPv4.</td>
152+
/// and will be resolved to an IPv4 address. In this case the remote server must allow access over IPv4.</td>
153153
/// </tr>
154154
/// <tr>
155155
/// <td><see cref="Protocol.Udp"/>, <see cref="Protocol.Tcp"/></td>
@@ -161,7 +161,7 @@ public interface INetworkLoggerSettings : ILoggerSettings
161161
/// <td><see cref="Protocol.Udp"/>, <see cref="Protocol.Tcp"/></td>
162162
/// <td><see cref="Address.IPv6"/></td><td>DNS&#160;name</td>
163163
/// <td>The <em>Host</em> property can contain a DNS host name, such as <c>localhost</c> for example,
164-
/// and will resolve to an IPv6 address. In this case the remote server must allow access over IPv6.</td>
164+
/// and will be resolved to an IPv6 address. In this case the remote server must allow access over IPv6.</td>
165165
/// </tr>
166166
/// <tr>
167167
/// <td><see cref="Protocol.Web"/></td><td>Ignored</td><td>URL</td>
@@ -173,7 +173,7 @@ public interface INetworkLoggerSettings : ILoggerSettings
173173
/// <see cref="Port"/> is set to zero.</li>
174174
/// <li>The port number is taken from property <see cref="Port"/> if no port is included in the URL and
175175
/// property <see cref="Port"/> is not set to zero.</li>
176-
/// <li>The port number is taken from the URL and if property <see cref="Port"/> is not set to zero.</li>
176+
/// <li>The port number is taken from the URL and if property <see cref="Port"/> is set to zero.</li>
177177
/// <li>The port number is taken from property <see cref="Port"/> if it is not set to zero and the port
178178
/// number in the URL is different from value of property <see cref="Port"/>.</li>
179179
/// </ul>
@@ -194,7 +194,8 @@ public interface INetworkLoggerSettings : ILoggerSettings
194194
/// Gets or sets the target port.
195195
/// </summary>
196196
/// <remarks>
197-
/// This property defines the port on which the target computer is listened to.
197+
/// This property defines the port on which the target computer is listening
198+
/// for logging requests.
198199
/// </remarks>
199200
/// <value>
200201
/// The target port to be used.
@@ -358,7 +359,7 @@ public interface INetworkLoggerSettings : ILoggerSettings
358359
/// </para>
359360
/// <para>
360361
/// Attention, any client connection is closed automatically if this property is set to
361-
/// <c>false</c> OR a message already ends with a zero termination! Otherwise the client
362+
/// <c>false</c> OR a message does not already end with a zero termination! Otherwise the client
362363
/// connection is kept open.
363364
/// </para>
364365
/// <para>
@@ -394,6 +395,32 @@ public interface INetworkLoggerSettings : ILoggerSettings
394395
/// </value>
395396
Int32 Timeout { get; set; }
396397

398+
/// <summary>
399+
/// Gets and set the HTTP request method to be used.
400+
/// </summary>
401+
/// <remarks>
402+
/// This property allows to change the the HTTP request method to be used. For
403+
/// the moment the method value is only used together with WEB requests!
404+
/// </remarks>
405+
/// <value>
406+
/// The HTTP request method to use, e.g. POST, PUT or any other valid WEB request
407+
/// method. The default is POST.
408+
/// </value>
409+
String Method { get; set; }
410+
411+
/// <summary>
412+
/// Gets and set the HTTP request content type to be used.
413+
/// </summary>
414+
/// <remarks>
415+
/// This property allows to change the the HTTP request content type to be used.
416+
/// For the moment the content value is only used together with WEB requests!
417+
/// </remarks>
418+
/// <value>
419+
/// The HTTP request content type to use, e.g. application/json, application/xml
420+
/// or any other valid WEB request content type. The default is application/json.
421+
/// </value>
422+
String Content { get; set; }
423+
397424
#endregion
398425
}
399426
}

code/src/Plexdata.LogWriter.Network/Internals/Sockets/WebClientSocket.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,9 @@ public Boolean Connect()
107107

108108
try
109109
{
110-
// Method "POST" in conjunction with "application/json" is used
111-
// for GELF requests, but might be used also for other requests.
112110
this.client = this.factory.Create<IClientFacade>(
113111
this.BuildRemoteUri(this.settings.Host, this.settings.Port),
114-
"POST", "application/json", this.settings.Timeout);
112+
this.settings.Method, this.settings.Content, this.settings.Timeout);
115113

116114
return true;
117115
}

code/src/Plexdata.LogWriter.Network/Settings/NetworkLoggerSettings.cs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,27 @@ public class NetworkLoggerSettings : LoggerSettings, INetworkLoggerSettings
124124
/// The default timeout value.
125125
/// </summary>
126126
/// <remarks>
127-
/// The default timeout value is set to 100000 milliseconds (100 seconds)
127+
/// The default timeout value is set to <c>100000</c> milliseconds (100 seconds)
128128
/// and has been taken from official Microsoft documentation.
129129
/// </remarks>
130130
private static readonly Int32 DefaultTimeout = 100000;
131131

132+
/// <summary>
133+
/// The default method value.
134+
/// </summary>
135+
/// <remarks>
136+
/// The default method value is set to <c>POST</c>.
137+
/// </remarks>
138+
private static readonly String DefaultMethod = "POST";
139+
140+
/// <summary>
141+
/// The default content type.
142+
/// </summary>
143+
/// <remarks>
144+
/// The default content type is set to <c>application/json</c>.
145+
/// </remarks>
146+
private static readonly String DefaultContent = "application/json";
147+
132148
#endregion
133149

134150
#region Private fields
@@ -223,6 +239,24 @@ public class NetworkLoggerSettings : LoggerSettings, INetworkLoggerSettings
223239
/// </remarks>
224240
private Int32 timeout;
225241

242+
/// <summary>
243+
/// This field holds the method value to be used.
244+
/// </summary>
245+
/// <remarks>
246+
/// The value of <see cref="NetworkLoggerSettings.DefaultMethod"/>
247+
/// is used as initial value.
248+
/// </remarks>
249+
private String method;
250+
251+
/// <summary>
252+
/// This field holds the content value to be used.
253+
/// </summary>
254+
/// <remarks>
255+
/// The value of <see cref="NetworkLoggerSettings.DefaultContent"/>
256+
/// is used as initial value.
257+
/// </remarks>
258+
private String content;
259+
226260
#endregion
227261

228262
#region Construction
@@ -254,6 +288,8 @@ public NetworkLoggerSettings()
254288
this.Maximum = NetworkLoggerSettings.DefaultMaximum;
255289
this.Termination = NetworkLoggerSettings.DefaultTermination;
256290
this.Timeout = NetworkLoggerSettings.DefaultTimeout;
291+
this.Method = NetworkLoggerSettings.DefaultMethod;
292+
this.Content = NetworkLoggerSettings.DefaultContent;
257293
}
258294

259295
/// <summary>
@@ -459,6 +495,40 @@ public Int32 Timeout
459495
}
460496
}
461497

498+
/// <inheritdoc />
499+
public String Method
500+
{
501+
get
502+
{
503+
return this.method;
504+
}
505+
set
506+
{
507+
if (this.method != value)
508+
{
509+
this.method = value;
510+
base.RaisePropertyChanged(nameof(this.Method));
511+
}
512+
}
513+
}
514+
515+
/// <inheritdoc />
516+
public String Content
517+
{
518+
get
519+
{
520+
return this.content;
521+
}
522+
set
523+
{
524+
if (this.Content != value)
525+
{
526+
this.content = value;
527+
base.RaisePropertyChanged(nameof(this.Content));
528+
}
529+
}
530+
}
531+
462532
#endregion
463533

464534
#region Protected Methods
@@ -496,6 +566,8 @@ protected override void LoadSettings(ILoggerSettingsSection configuration)
496566
this.Maximum = base.GetValue(section[nameof(this.Maximum)], NetworkLoggerSettings.DefaultMaximum);
497567
this.Termination = base.GetValue(section[nameof(this.Termination)], NetworkLoggerSettings.DefaultTermination);
498568
this.Timeout = base.GetValue(section[nameof(this.Timeout)], NetworkLoggerSettings.DefaultTimeout);
569+
this.Method = base.GetValue(section[nameof(this.Method)], NetworkLoggerSettings.DefaultMethod);
570+
this.Content = base.GetValue(section[nameof(this.Content)], NetworkLoggerSettings.DefaultContent);
499571
}
500572

501573
#endregion

code/src/Plexdata.LogWriter.Testing.Helper/Plexdata.LogWriter.Testing.Helper.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
<Project>{726C7B51-8A48-4008-A47D-0B8467B9887F}</Project>
7474
<Name>Plexdata.LogWriter.Mail</Name>
7575
</ProjectReference>
76+
<ProjectReference Include="..\Plexdata.LogWriter.Network\Plexdata.LogWriter.Network.csproj">
77+
<Project>{FF61AF1E-E0D3-425F-B5C4-8EC9BA573AC6}</Project>
78+
<Name>Plexdata.LogWriter.Network</Name>
79+
</ProjectReference>
7680
<ProjectReference Include="..\Plexdata.LogWriter.Persistent\Plexdata.LogWriter.Persistent.csproj">
7781
<Project>{B38DA898-A77E-4988-BEA0-5E69839D1499}</Project>
7882
<Name>Plexdata.LogWriter.Persistent</Name>

0 commit comments

Comments
 (0)