Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net method GetLogger() Equivalent in Serilog

Brief Backround

We are replacing in our project the log4net to Serilog. In many of our classes we are using the method LogManager.GetLogger of log4net when giving as a parameter the class name using MethodBase.GetCurrentMethod().DeclaringType, so we could know in which class the log was written.

My Question Is

Is there any method which is equivalent in Serilog which can give me the logger? I know that there is the option of ForContext() which you can specify the class you're in, but I still don't know how to get an instance of a logger without some kind of logManager.
P.S
To give a logger as a parameter to each class constructor (instead of using Getlogger()) would take some time and I would really prefer to avoid this option.

like image 794
Green Avatar asked Oct 21 '25 19:10

Green


2 Answers

After I did some research, I founded out that there is NO Log4Net GetLogger() equivalent in Serilog.

like image 56
Green Avatar answered Oct 23 '25 11:10

Green


Answer is NO, but maybe Source Contexts is what you needed.

There is an example from Serilog's Wiki:

var myLog = Log.ForContext<MyClass>();
myLog.Information("Hello!");
like image 42
gsw945 Avatar answered Oct 23 '25 10:10

gsw945



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!