This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for durable logging is based on the implementation of the Seq sink. ControlledSwithc, ExponentialBackoff and PortableTimes have been imported directly (except for the namespace change. LogglySink uses the same strategy of Seq Sink to determine if the client is durable or not. HttpLogShipper uses the LogglyClient from the loggly-csharp lib instead of the http client directly, and can use the bulk api correctly. DurableSink uses rolling file sink (like Seq's durableSink) to store events, but serializes them as logglyEvents instead of serilog's logevent. This avoid problems reading the serialized events from the file. Event conversion is in LogEventConverter class for reuse
- Loading branch information
Miguel Alho
committed
Dec 15, 2016
1 parent
15421f6
commit ff87021
Showing
16 changed files
with
1,177 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Loggly; | ||
using Loggly.Config; | ||
using Serilog; | ||
using Serilog.Core.Enrichers; | ||
using Serilog.Enrichers; | ||
using Serilog.Sinks.RollingFileAlternate; | ||
|
||
namespace sampleDurableLogger | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
SetupLogglyConfiguration(); | ||
var logger = CreateLoggerFactory(@"c:\test\").CreateLogger(); | ||
|
||
logger.Information((Exception)null, "test message - app started"); | ||
logger.Warning((Exception)null, "test message with {@data}", new { p1 = "sample", p2=DateTime.Now }); | ||
logger.Warning((Exception)null, "test2 message with {@data}", new { p1 = "sample2", p2 = 10 }); | ||
|
||
Console.WriteLine("disconnect to test offline. Two messages will be sent. Press enter to send and wait a minute or so before reconnecting or use breakpoints to see that send fails."); | ||
Console.ReadLine(); | ||
|
||
logger.Information((Exception)null, "second test message - app started"); | ||
logger.Warning((Exception)null, "second test message with {@data}", new { p1 = "sample2", p2 = DateTime.Now }); | ||
|
||
|
||
Console.WriteLine("Offline messages written. Once you have confirmed that messages have been written locally, reconnect to see messages go out. Press Enter for more messages to be written."); | ||
Console.ReadLine(); | ||
|
||
logger.Information((Exception)null, "third test message - app started"); | ||
logger.Warning((Exception)null, "third test message with {@data}", new { p1 = "sample3", p2 = DateTime.Now }); | ||
|
||
Console.WriteLine("back online messages written. Check loggly and files for data. wait a minute or so before reconnecting. Press Enter to temrinate"); | ||
Console.ReadLine(); | ||
} | ||
|
||
public static LoggerConfiguration CreateLoggerFactory(string logFilePath) | ||
{ | ||
return new LoggerConfiguration() | ||
.MinimumLevel.Debug() | ||
//Add enrichers | ||
.Enrich.FromLogContext() | ||
.Enrich.WithProcessId() | ||
.Enrich.WithThreadId() | ||
.Enrich.With(new EnvironmentUserNameEnricher()) | ||
.Enrich.With(new MachineNameEnricher()) | ||
.Enrich.With(new PropertyEnricher("Environment", GetLoggingEnvironment())) | ||
//Add sinks | ||
.WriteTo.Async(s => s.Loggly( | ||
bufferBaseFilename: logFilePath + "buffer") | ||
.MinimumLevel.Information() | ||
) | ||
.WriteTo.Async(s => s.RollingFileAlternate( | ||
logFilePath, | ||
outputTemplate: | ||
"[{ProcessId}] {Timestamp:yyyy-MM-dd HH:mm:ss.fff K} [{ThreadId}] [{Level}] [{SourceContext}] [{Category}] {Message}{NewLine}{Exception}", | ||
fileSizeLimitBytes: 10 * 1024 * 1024, | ||
retainedFileCountLimit: 100) | ||
.MinimumLevel.Debug() | ||
); | ||
} | ||
|
||
private static string GetLoggingEnvironment() | ||
{ | ||
return "development"; | ||
} | ||
|
||
private static void SetupLogglyConfiguration() | ||
{ | ||
///CHANGE THESE TOO TO YOUR LOGGLY ACCOUNT: DO NOT COMMIT TO Source control!!! | ||
const string appName = "AppNameHere"; | ||
const string customerToken = "yourkeyhere"; | ||
|
||
//Configure Loggly | ||
var config = LogglyConfig.Instance; | ||
config.CustomerToken = customerToken; | ||
config.ApplicationName = appName; | ||
config.Transport = new TransportConfiguration() | ||
{ | ||
EndpointHostname = "logs-01.loggly.com", | ||
EndpointPort = 443, | ||
LogTransport = LogTransport.Https | ||
}; | ||
config.ThrowExceptions = true; | ||
|
||
//Define Tags sent to Loggly | ||
config.TagConfig.Tags.AddRange(new ITag[]{ | ||
new ApplicationNameTag {Formatter = "application-{0}"}, | ||
new HostnameTag { Formatter = "host-{0}" } | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("Microsoft")] | ||
[assembly: AssemblyProduct("sampleDurableLogger")] | ||
[assembly: AssemblyTrademark("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("a47ed1ce-fe7c-444e-9391-10d6b60519c2")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"version": "1.0.0-*", | ||
"buildOptions": { | ||
"emitEntryPoint": true | ||
}, | ||
|
||
"dependencies": { | ||
"Microsoft.NETCore.App": { | ||
"type": "platform", | ||
"version": "1.0.0" | ||
}, | ||
"loggly-csharp": "4.6.0.5", | ||
"Newtonsoft.Json": "9.0.1", | ||
"Serilog": "2.3.0", | ||
"Serilog.Sinks.PeriodicBatching": "2.0.0", | ||
"Serilog.Sinks.File": "3.1.1", | ||
"Serilog.Sinks.RollingFile": "3.2.0", | ||
"Serilog.Sinks.RollingFileAlternate": "2.0.6", | ||
"Serilog.Sinks.Async": "1.0.1", | ||
"Serilog.Enrichers.Environment": "2.1.1", | ||
"Serilog.Enrichers.Process": "2.0.0", | ||
"Serilog.Enrichers.Thread": "3.0.0", | ||
"Serilog.Sinks.Loggly": "3.0.3-*" | ||
}, | ||
|
||
"frameworks": { | ||
"netcoreapp1.0": { | ||
"imports": "dnxcore50" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
|
||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>a47ed1ce-fe7c-444e-9391-10d6b60519c2</ProjectGuid> | ||
<RootNamespace>sampleDurableLogger</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | ||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Serilog.Sinks.Seq Copyright 2016 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Sinks.Loggly | ||
{ | ||
/// <summary> | ||
/// Instances of this type are single-threaded, generally only updated on a background | ||
/// timer thread. An exception is <see cref="IsIncluded(LogEvent)"/>, which may be called | ||
/// concurrently but performs no synchronization. | ||
/// </summary> | ||
class ControlledLevelSwitch | ||
{ | ||
// If non-null, then background level checks will be performed; set either through the constructor | ||
// or in response to a level specification from the server. Never set to null after being made non-null. | ||
LoggingLevelSwitch _controlledSwitch; | ||
LogEventLevel? _originalLevel; | ||
|
||
public ControlledLevelSwitch(LoggingLevelSwitch controlledSwitch = null) | ||
{ | ||
_controlledSwitch = controlledSwitch; | ||
} | ||
|
||
public bool IsActive => _controlledSwitch != null; | ||
|
||
public bool IsIncluded(LogEvent evt) | ||
{ | ||
// Concurrent, but not synchronized. | ||
var controlledSwitch = _controlledSwitch; | ||
return controlledSwitch == null || | ||
(int)controlledSwitch.MinimumLevel <= (int)evt.Level; | ||
} | ||
|
||
public void Update(LogEventLevel? minimumAcceptedLevel) | ||
{ | ||
if (minimumAcceptedLevel == null) | ||
{ | ||
if (_controlledSwitch != null && _originalLevel.HasValue) | ||
_controlledSwitch.MinimumLevel = _originalLevel.Value; | ||
|
||
return; | ||
} | ||
|
||
if (_controlledSwitch == null) | ||
{ | ||
// The server is controlling the logging level, but not the overall logger. Hence, if the server | ||
// stops controlling the level, the switch should become transparent. | ||
_originalLevel = LevelAlias.Minimum; | ||
_controlledSwitch = new LoggingLevelSwitch(minimumAcceptedLevel.Value); | ||
return; | ||
} | ||
|
||
if (!_originalLevel.HasValue) | ||
_originalLevel = _controlledSwitch.MinimumLevel; | ||
|
||
_controlledSwitch.MinimumLevel = minimumAcceptedLevel.Value; | ||
} | ||
} | ||
} |
Oops, something went wrong.