Skip to content

Commit

Permalink
When reading the config for creating the installation do it with file…
Browse files Browse the repository at this point in the history
… read over using IConfiguration which may throw an exception
  • Loading branch information
ThomasArdal committed Nov 7, 2024
1 parent e07487b commit 31e4e8b
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/Elmah.Io.AspNetCore/MessageShipper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
Expand All @@ -9,10 +10,9 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Elmah.Io.AspNetCore
{
Expand Down Expand Up @@ -115,25 +115,23 @@ public static void CreateInstallation(IApplicationBuilder app)
Loggers = [logger]
};

try
var location = typeof(MessageShipper).Assembly.Location;
var currentDirectory = Path.GetDirectoryName(location);
var appsettingsFilePath = Path.Combine(currentDirectory, "appsettings.json");
if (File.Exists(appsettingsFilePath))
{
var configuration = app.ApplicationServices.GetService<IConfiguration>();
var elmahio = configuration.GetSection("ElmahIo").Get<ElmahIoOptions>();
if (elmahio != null)
var appsettingsContent = File.ReadAllText(appsettingsFilePath);
var appsettingsObject = JObject.Parse(appsettingsContent);
if (appsettingsObject.TryGetValue("ElmahIo", out JToken elmahIoSection))
{
logger.ConfigFiles.Add(new ConfigFile
{
Name = "appsettings.json",
Content = JsonConvert.SerializeObject(elmahio),
ContentType = "application/json",
Name = Path.GetFileName(appsettingsFilePath),
Content = new JObject { { "ElmahIo", elmahIoSection.DeepClone() } }.ToString(),
ContentType = "application/json"
});
}
}
catch
{
// There might be a problem with the config. Since we still reached this line the application
// seem to start up. So, let us create the installation without the config file.
}

var elmahioApi = ElmahioAPI.Create(options.ApiKey, new Client.ElmahIoOptions
{
Expand Down

0 comments on commit 31e4e8b

Please sign in to comment.