Skip to content

Commit

Permalink
Re-introduced WriteTo method accepting api key and log id as strings …
Browse files Browse the repository at this point in the history
…to support configuration through appsettings.json and similar
  • Loading branch information
ThomasArdal committed Oct 19, 2023
1 parent 2481426 commit 7821736
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/Serilog.Sinks.ElmahIo/LoggerConfigurationElmahIOExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2014 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.
Expand Down Expand Up @@ -54,5 +54,32 @@ public static LoggerConfiguration ElmahIo(
restrictedToMinimumLevel: options.MinimumLogEventLevel ?? LevelAlias.Minimum,
levelSwitch: options.LevelSwitch);
}

/// <summary>
/// Adds a sink that writes log events to elmah.io. This overload accepts logId as a string and
/// should be used from packages not supporting ElmahIoSinkOptions (like when configuring
/// through appsettings.json files or similar.
/// </summary>
/// <param name="loggerConfiguration">The logger configuration.</param>
/// <param name="apiKey">An API key from the organization containing the log.</param>
/// <param name="logId">The log ID as found on the elmah.io website.</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink. Set to Verbose by default.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
[Obsolete("This method is intended for configuration through appsettings.json or similar only. From C# you should always call the overload accepting ElmahIoSinkOptions.")]
public static LoggerConfiguration ElmahIo(
this LoggerSinkConfiguration loggerConfiguration,
string apiKey,
string logId,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null)
{
return ElmahIo(loggerConfiguration, new ElmahIoSinkOptions(apiKey, new Guid(logId))
{
MinimumLogEventLevel = restrictedToMinimumLevel,
FormatProvider = formatProvider,
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Microsoft.Extensions.Configuration;
using NUnit.Framework;
using Serilog.Core;
using System;
using System.Linq;
using System.Reflection;

namespace Serilog.Sinks.ElmahIo.Tests
{
public class LoggerConfigurationElmahIoExtensionsTest
{
[Test]
public void CanConfigureFromOptions()
{
var log = new LoggerConfiguration()
.WriteTo.ElmahIo(new ElmahIoSinkOptions("apiKey", Guid.NewGuid()))
.CreateLogger();

AssertInstalledSink(log);
}

[Test]
public void CanConfigureFromStringAndGuid()
{
#pragma warning disable CS0618 // Type or member is obsolete
var log = new LoggerConfiguration()
.WriteTo.ElmahIo("apiKey", Guid.NewGuid().ToString())
.CreateLogger();
#pragma warning restore CS0618 // Type or member is obsolete

AssertInstalledSink(log);
}

[Test]
public void CanConfigureFromAppsettings()
{
var configuration = new ConfigurationBuilder()
.AddObject(new
{
Serilog = new
{
Using = new[] { "Serilog.Sinks.ElmahIo" },
MinimumLevel = new
{
Default = "Information"
},
WriteTo = new[]
{
new
{
Name = "ElmahIo",
Args = new
{
apiKey = "API_KEY",
logId = Guid.NewGuid().ToString(),
}
}
}
}
})
.Build();

var log = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();

AssertInstalledSink(log);
}

private static void AssertInstalledSink(Logger log)
{
var aggregateSinkFieldInfo = log
.GetType()
.GetField("_sink", BindingFlags.Instance | BindingFlags.NonPublic);
var aggregateSink = (ILogEventSink)aggregateSinkFieldInfo?.GetValue(log);
var sinkEnumerableFieldInfo = aggregateSink?
.GetType()
.GetField("_sinks", BindingFlags.Instance | BindingFlags.NonPublic);
var sinks = (ILogEventSink[])sinkEnumerableFieldInfo?.GetValue(aggregateSink);
Assert.That(sinks.Count, Is.EqualTo(1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Extensions.Configuration.Object" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
Expand Down

0 comments on commit 7821736

Please sign in to comment.