Using rolling file for information and email for errors

1,052 views
Skip to first unread message

David Smith

unread,
Jun 9, 2015, 9:11:47 PM6/9/15
to ser...@googlegroups.com

How can I, a Serilog newbie, configure Serilog to write info (and errors) to a rolling file in the App_Data directory of an ASP.NET WebApi 2 app and email errors to a specific address?

Here are the details of my question:

  1. Right now I've added the rolling file logger via web.config. To add multiple loggers, do I need to do that in code?
  2. Looks like I need to specify the path for the rolling file logger as an absolute path, but I would like to specify "~/App_Data" and have the app resolve that. How can I achieve that?
  3. How do I configure Serilog to mail just errors to me? Do I have to write my own email sink that ignores all messages other than errors?
  4. I tried adding the Serilog.Sink.Email package, but running my app yields a versioning exception, something about being unable to bind to Serilog 1.4.0.0. Removing the configuration of the email package removed the error, so I moved on.

While I appreciate the existing documentation, I would like to see some more complex/realistic examples. For instance, what are the best practices around dependency injection? Will I run into collisions if my app instantiates multiple ILoggers? Is there a sample app that wires up multiple sinks? What should I avoid doing with Serilog?

Beyond that, it seems like a great project. I just need more guidance to use it in a production app.

nblum...@nblumhardt.com

unread,
Jun 10, 2015, 6:08:10 AM6/10/15
to ser...@googlegroups.com, da...@dreamofblue.com
Hi David,

Thanks for dropping by!

1. You can configure multiple loggers of different types in Web.config; multiple loggers of the same type need to be configured in code
2. Using an absolute path is recommended, but you can achieve this by configuring the sink in code and using Server.MapPath("~/App_Data") to get the full path
3. Use the restrictedToMinimumLevel parameter on the sink's configuration method, or serilog:write-to:SinkName.restrictedToMinimumLevel in XML
4. Installing the Serilog.Sinks.Email NuGet package should add a binding redirect to the Web.config file like the one below - can you spot anything out of the ordinary?

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Serilog.FullNetFx" publicKeyToken="24c2f752a8e58a10" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.0.0" newVersion="1.5.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Serilog" publicKeyToken="24c2f752a8e58a10" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.0.0" newVersion="1.5.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Cheers,
Nick

David Smith

unread,
Jun 10, 2015, 7:38:32 AM6/10/15
to ser...@googlegroups.com, da...@dreamofblue.com
No, I don't see any assembly redirection added to my web.config after adding the email package. I added your XML to my web.config and it worked, thanks. Might be something to look into regarding the Serilog.Sink.Email package.

Regarding Server.MapPath, is there a way I can reconfigure the rolling file logger after the initial configuration, like reach back into the sinks and change the path after Server.MapPath is available? Right now I have the path configured in XML because configuring multiple loggers wasn't working for me. Nothing was being logged, but that may have been because I was trying to write to ~/App_Data.

David Smith

unread,
Jun 10, 2015, 8:32:46 AM6/10/15
to ser...@googlegroups.com
And, no sooner have I said it works, I am now no longer seeing the error email. Even in my spam folder. What happens if sending the email fails?

David Smith

unread,
Jun 10, 2015, 8:50:19 AM6/10/15
to ser...@googlegroups.com
Seems to ignore the email logger when I configure in web.config but not when I configure the same values in code. Here is my web.config snippet:

    <add key="serilog:write-to:Email.restrictedToMinimumLevel" value="Error" />
    <add key="serilog:write-to:Email.from" value="err...@example.com" />
    <add key="serilog:write-to:Email.to" value="t...@example.com" />
    <add key="serilog:write-to:Email.mailServer" value="smtp.example.com" />
    <add key="serilog:write-to:Email.emailSubject" value="Address Verification Service Error" />

nblum...@nblumhardt.com

unread,
Jun 11, 2015, 3:48:49 AM6/11/15
to ser...@googlegroups.com, da...@dreamofblue.com
Aha - I think you're missing:

<add key="serilog:using:Email" value="Serilog.Sinks.Email" />

Also making sure the Serilog.Sinks.Email.dll binary ends up in the output folder is worth checking.

Hope this helps!
Reply all
Reply to author
Forward
0 new messages