Anotar.Serilog.Fody cause System.NullReferenceException in static constructer.
See original GitHub issueAnotar.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:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
can u try 5.1.2?
This is now deployed. NuGet may take some time to make it available for download.