Skip to content

Commit

Permalink
Create installation
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasArdal committed Nov 7, 2024
1 parent 3948591 commit 003899c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Elmah.Io.Functions/ElmahIoExceptionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ElmahIoExceptionFilter : IFunctionExceptionFilter, IFunctionInvocat
public ElmahIoExceptionFilter(IOptions<ElmahIoFunctionOptions> options)
{
this.options = options.Value;
MessageShipper.CreateInstallation(this.options);
}

/// <summary>
Expand Down
72 changes: 62 additions & 10 deletions src/Elmah.Io.Functions/MessageShipper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Elmah.Io.Functions
internal static class MessageShipper
{
private static readonly string _assemblyVersion = typeof(MessageShipper).Assembly.GetName().Version.ToString();
private static readonly string _elmahIoClientAssemblyVersion = typeof(IElmahioAPI).Assembly.GetName().Version.ToString();
#pragma warning disable CS0618 // Type or member is obsolete
private static readonly string _functionsAssemblyVersion = typeof(FunctionExceptionContext).Assembly.GetName().Version.ToString();

Expand Down Expand Up @@ -46,6 +47,67 @@ public static async Task Ship(FunctionExceptionContext exceptionContext, HttpCon
Application = options.Application,
};

EnsureClient(options);

try
{
await elmahIoClient.Messages.CreateAndNotifyAsync(options.LogId, createMessage, cancellationToken);
}
catch (Exception e)
{
options.OnError?.Invoke(createMessage, e);
// If there's a Exception while generating the error page, re-throw the original exception.
}
}

public static void CreateInstallation(ElmahIoFunctionOptions options)
{
try
{
var logger = new LoggerInfo
{
Type = "Elmah.Io.Functions",
Assemblies =
[
new AssemblyInfo
{
Name = "Elmah.Io.Functions",
Version = _assemblyVersion,
},
new AssemblyInfo
{
Name = "Elmah.Io.Client",
Version = _elmahIoClientAssemblyVersion,
},
new AssemblyInfo
{
Name = "Microsoft.Azure.WebJobs.Host",
Version = _functionsAssemblyVersion,
}
],
ConfigFiles = [],
Properties = [],
};

var installation = new CreateInstallation
{
Name = options.Application,
Type = "azurefunction",
Loggers = [logger]
};

EnsureClient(options);

elmahIoClient.Installations.Create(options.LogId.ToString(), installation);
}
catch
{
// We don't want to crash the entire application if the installation fails. Carry on.
}
}

private static void EnsureClient(ElmahIoFunctionOptions options)
{
if (elmahIoClient == null)
{
elmahIoClient = ElmahioAPI.Create(options.ApiKey, new ElmahIoOptions
Expand All @@ -72,16 +134,6 @@ public static async Task Ship(FunctionExceptionContext exceptionContext, HttpCon
options.OnError?.Invoke(args.Message, args.Error);
};
}

try
{
await elmahIoClient.Messages.CreateAndNotifyAsync(options.LogId, createMessage, cancellationToken);
}
catch (Exception e)
{
options.OnError?.Invoke(createMessage, e);
// If there's a Exception while generating the error page, re-throw the original exception.
}
}

/// <summary>
Expand Down

0 comments on commit 003899c

Please sign in to comment.