question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Anotar.Serilog.Fody cause System.NullReferenceException in static constructer.

See original GitHub issue

Anotar.Serilog.Fody cause System.NullReferenceException in static constructer.

Can I use in static constructer?

Core code is here.

    static class StaticClassFail
    {
        static StaticClassFail()
        {
            //Success
            Serilog.Log.Information("Static Constructor from Plane-Serilog");
            //Exception!
            Anotar.Serilog.LogTo.Information("Static Constructor from Fody-Serilog");
        }
    }
  • Log setup is already finished in Program class constructer.

  • It is not cause in Normal class.

  • It is not cause in normal static method.

  • It is not cause in plane Serilog.

Total code is here.

using Serilog;
using System;

namespace FodySerilogErrorSample
{
    class Program
    {
        static Program()
        {
            SetupLoggerConfig();
        }

        static void Main(string[] args)
        {
            //Success
            var normal = new NormalClass();

            //Success
            StaticClassSuccess.WriteLog();

            //Exception!
            var failCall = StaticClassFail.PropertyForCallConstructor;

            Console.Read();
        }

        private static void SetupLoggerConfig()
        {
            string template = "| {Timestamp:HH:mm:ss.fff} | {Message:j} | {SourceContext} | {MethodName} | {LineNumber} L|{NewLine}{Exception}";

            Serilog.Log.Logger = new LoggerConfiguration()
                            .WriteTo.Console(outputTemplate: template)
                            .CreateLogger();
        }
    }

    class NormalClass
    {
        public NormalClass()
        {
            //Success
            Anotar.Serilog.LogTo.Information("Normal Constructor");
        }
    }

    static class StaticClassFail
    {
        public static string PropertyForCallConstructor { get; set; }

        static StaticClassFail()
        {
            //Success
            Serilog.Log.Information("Static Constructor from Plane-Serilog");
            //Exception!
            Anotar.Serilog.LogTo.Information("Static Constructor from Fody-Serilog");
        }
    }

    static class StaticClassSuccess
    {
        public static void WriteLog()
        //Success
            => Anotar.Serilog.LogTo.Information("Static Method");
    }
}

Console Output

| 22:37:51.362 | Normal Constructor | FodySerilogErrorSample.NormalClass | Void .ctor() | 42 L|
| 22:37:51.391 | Static Method | FodySerilogErrorSample.StaticClassSuccess | Void WriteLog() | 63 L|
| 22:37:51.393 | Static Constructor from Plane-Serilog |  |  |  L|

Exception Detail

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=FodySerilogErrorSample
  Stuck Trace:
   at FodySerilogErrorSample.StaticClassFail..cctor() in D:\Documents\VSProjects\FodySerilogErrorSample\FodySerilogErrorSample\Program.cs:line 55

Environment

VisualStudio2019 .NET Core 3.1 C#8

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Anotar.Serilog.Fody" Version="5.1.0" />
    <PackageReference Include="Serilog" Version="2.9.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
  </ItemGroup>
</Project>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SimonCroppcommented, May 8, 2020

can u try 5.1.2?

1reaction
SimonCroppcommented, May 5, 2020

This is now deployed. NuGet may take some time to make it available for download.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does the '{' throw a NullReferenceException in a static ...
I'm guessing that the line numbers are off (the actual reason for that is not as important) and the exception is actually thrown...
Read more >
Newest 'fody' Questions - Page 3
I am using Fody. I created the BaseViewModel Successfully and here is it: using System.ComponentModel; namespace GProject_MVVM.
Read more >
SuperMemoAssistant.Interop 2.1.0-beta.17
Enables interactions between SuperMemo Assistant and Plugins.
Read more >
The Tidelift catalog of open source packages
An SDK to build distributed and decentralized computing projects. This project was created in order to support a new internet. One that is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found