serilog-sinks-email icon indicating copy to clipboard operation
serilog-sinks-email copied to clipboard

Subject cannot be set via json config, when using the batching option

Open datobu opened this issue 10 months ago • 4 comments

We would like to set a custom subject to our serilog emails while using batching options. We configure our complete serilog within a json config file.

The subject has always the default value, because apparently the input of the json is not currently parsed through a MessageTemplateTextFormatter.

Our workaround is to manually build the logger in the code in the meantime. However, it would be great if this could be fixed.

If you need further input, don't hesitate to reach out.

datobu avatar Jan 24 '25 10:01 datobu

I am able to replicate this bug on my end. Will attempt defining the email sink in Code as you did. Thank you for creating this issue.

Shaalan15 avatar Feb 27 '25 09:02 Shaalan15

Hello community,

I have the same problem with the NuGet package version 4.1.0, but I do not use batching. All emails I receive have "Log Messages" as subject.

Best regards

fnick avatar Jun 26 '25 18:06 fnick

There is difference when using options and just straight args when configuring the email sink

Email(from, to, host, port, connectionSecurity, credentials, subject, body, formatProvider, restrictedToMinimumLevel, levelSwitch)
Email(options, batchingOptions, restrictedToMinimumLevel, levelSwitch)

With options you there is property for subject but it is set to DefaultSubject and doesn't seem to get serialized from appsettings. And if you set under options in appsetting the subject, you wil get error

Serilog.Settings.Configuration: Property setter on Serilog.Sinks.Email.EmailSinkOptions failed: System.InvalidOperationException: Type Watcher virhe was not found.

So I'm guessing I should have some class somewhere that is same as the subject???

public ITextFormatter Subject { get; set; } = new MessageTemplateTextFormatter(DefaultSubject);

{
  "Name": "Email",
  "Args": {
    "options": {
     "from": "[email protected]",
      "to": [ "[email protected]" ],
      "host": "hostaddress",
      "restrictedToMinimumLevel": "Warning",
      "outputTemplate": "{Timestamp:s} [{Level:u3}] {Message}{NewLine}{Exception}"
    },
    "batchingOptions": {
      "batchSizeLimit": 1,
      "period": "00:01:00"
    },
    "restrictedToMinimumLevel": "Warning"
  }
},

With args you can give subject

{
  "Name": "Email",
  "Args": {
    "from": "[email protected]",
    "to": "[email protected]",
    "host": "hostaddress",
    "subject": "Your subject",
    "restrictedToMinimumLevel": "Warning",
    "outputTemplate": "{Timestamp:s} [{Level:u3}] {Message}{NewLine}{Exception}"
  }
}

jyrijh avatar Sep 23 '25 10:09 jyrijh

So to get the Subject to work with options you need to create your own class like this

using Serilog.Events;
using Serilog.Formatting;
using System.IO;

namespace FolderWatcher;
public class WatcherSubject: ITextFormatter
{
    public void Format(LogEvent logEvent, TextWriter output)
    {
       output.WriteLine($"{AppDomain.CurrentDomain.FriendlyName} {logEvent.Level}");
    }
}

And in appsetting the subject is fully qualified type name "subject": "FolderWatcher.WatcherSubject, FolderWatcher"

{
  "Name": "Email",
  "Args": {
    "options": {
     "from": "[email protected]",
      "to": [ "[email protected]" ],
      "host": "hostaddress",
      "subject": "FolderWatcher.WatcherSubject, FolderWatcher",
      "restrictedToMinimumLevel": "Warning",
      "outputTemplate": "{Timestamp:s} [{Level:u3}] {Message}{NewLine}{Exception}"
    },
    "batchingOptions": {
      "batchSizeLimit": 1,
      "period": "00:01:00"
    },
    "restrictedToMinimumLevel": "Warning"
  }
},

jyrijh avatar Sep 23 '25 11:09 jyrijh