Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

add sample that configures sink with appsettings.json #53

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions sample/SampleAppSettingsConfig/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Globalization;
using Microsoft.Extensions.Configuration;
using Serilog;
using Serilog.Context;
using Serilog.Core;

namespace SampleAppSettingsConfig
{
public class Program
{
public static void Main()
{
//SetupLogglyConfiguration();
using (var logger = CreateLogger(@"C:\test\Logs\"))
{
//The messages being used here are the same as the set in the SampleDurableLoggle project
logger.Information("Test message - app started");

logger.Warning("Test message with {@Data}", new { P1 = "sample", P2 = DateTime.Now });
logger.Warning("Test2 message with {@Data}", new { P1 = "sample2", P2 = 10 });
logger.Information("Second test message");
logger.Warning("Second test message with {@Data}", new { P1 = "sample2", P2 = DateTime.Now, P3 = DateTime.UtcNow, P4 = DateTimeOffset.Now, P5 = DateTimeOffset.UtcNow });
logger.Information("Third test message");
logger.Warning("Third test message with {@Data}", new { P1 = "sample3", P2 = DateTime.Now });
using (LogContext.PushProperty("sampleProperty", "Sample Value"))
{
logger.Information("message to send with {@Data}", new { P1 = "sample4", P2 = DateTime.Now });
}
Console.WriteLine(
"Pushed property added to object. Check loggly and data. Press Enter to terminate");
Console.ReadLine();
}
}

static Logger CreateLogger(string logFilePath)
{
//write selflog to stderr
Serilog.Debugging.SelfLog.Enable(Console.Error);

var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();

return new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.MinimumLevel.Debug()
.CreateLogger();
}
}
}
27 changes: 27 additions & 0 deletions sample/SampleAppSettingsConfig/SampleAppSettingsConfig.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Serilog.Sinks.Loggly\Serilog.Sinks.Loggly.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.1" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions sample/SampleAppSettingsConfig/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.Loggly"
],
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "Loggly",
"Args": {
"tags": "someSampleTag",
"customerToken": "<yourKeyHere>",
"endpointHostName": "logs-01.loggly.com",
"isEnabled": "true"
}
}
],
"Enrich": [
"FromLogContext",
"WithExceptionDetails",
"WithThreadId"
],
"Properties": {
"Application": "SampleAppSettingsConfig",
"Environment": "someSampleTag"
}
}
}
14 changes: 12 additions & 2 deletions serilog-sinks-loggly.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.12
# Visual Studio Version 16
VisualStudioVersion = 16.0.29613.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{037440DE-440B-4129-9F7A-09B42D00397E}"
EndProject
Expand All @@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.Loggly.Tests"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sampleDurableLogger", "sample\sampleDurableLogger\sampleDurableLogger.csproj", "{A47ED1CE-FE7C-444E-9391-10D6B60519C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleAppSettingsConfig", "sample\SampleAppSettingsConfig\SampleAppSettingsConfig.csproj", "{FB985371-5BEF-4738-85F0-45779E59D56E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -42,6 +44,10 @@ Global
{A47ED1CE-FE7C-444E-9391-10D6B60519C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A47ED1CE-FE7C-444E-9391-10D6B60519C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A47ED1CE-FE7C-444E-9391-10D6B60519C2}.Release|Any CPU.Build.0 = Release|Any CPU
{FB985371-5BEF-4738-85F0-45779E59D56E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB985371-5BEF-4738-85F0-45779E59D56E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB985371-5BEF-4738-85F0-45779E59D56E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB985371-5BEF-4738-85F0-45779E59D56E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -50,5 +56,9 @@ Global
{94E6E098-11A0-43CF-B0CF-4BA270CE9DBD} = {037440DE-440B-4129-9F7A-09B42D00397E}
{120C431E-479C-48C7-9539-CFA32399769C} = {2B558B69-8F95-4F82-B223-EBF60F6F31EE}
{A47ED1CE-FE7C-444E-9391-10D6B60519C2} = {FD377716-21BA-45D1-9580-02C2BECA5BAB}
{FB985371-5BEF-4738-85F0-45779E59D56E} = {FD377716-21BA-45D1-9580-02C2BECA5BAB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0E5AE13F-FE25-44B3-8F9B-70A4298A6128}
EndGlobalSection
EndGlobal