nreco / logging

Generic file logger for .NET Core (FileLoggerProvider) with minimal dependencies
MIT License
284 stars 56 forks source link

Try Catch does not log #51

Open ShaunStanley opened 1 year ago

ShaunStanley commented 1 year ago

Hi,

I am new to logging and currently exploring logging frameworks, while testing out this framework, when I have a try catch condition, it seems like the Exception log is not logged. Perhaps anyone could advise regarding this matter?

Here is my test code (using MVC architecture and this section of code is in the controller): public async Task TestError() { int statCode = 0; string msg = ""; try { Guid g = new Guid("ABC"); //Here it will definitely create an error since "ABC" is not a proper Guid format statCode = 200; msg = g.ToString(); } catch (Exception ex) { statCode = 500; msg = ex.ToString(); } return StatusCode(statCode, msg); }

Thanks in advance.

VitaliyMF commented 1 year ago

Do you mean that you see an error log entry in, say, console but not in the log file? What are your log levels in appsettings.json?

ShaunStanley commented 1 year ago

Yup.

For the log level, it is the same as what was documented in the example project you included in this repository.

Anyways, here is the appsettings.json:

{ "Logging": { "LogLevel": { "Default": "Debug", "System": "Warning", "Microsoft": "Error" }, "FileOne": { "Path": "logs/app.log", "Append": "true", "MinLevel": "Information" }, "FileTwo": { "Path": "logs/app_debug.log", "Append": "true", "MinLevel": "Debug" }, "FileThree": { "Path": "logs/app_Error.log", "Append": "true", "MinLevel": "Error" }, "FileFour": { "Path": "logs/app_TestController.log", "Append": "true", "MinLevel": "Error" }, "FileFive": { "Path": "logs/app_TestControllerLuL.log", "Append": "true", "MinLevel": "Error" } }, "AllowedHosts": "*" }

VitaliyMF commented 1 year ago

And you have made necessary AddFile calls for all these sections ("FileOne", "FileTwo" etc) and your nuget reference is for the latest version (1.1.6)? Could you confirm that the same happens in https://github.com/nreco/logging/tree/master/examples/TwoLogFilesMvc ?

ShaunStanley commented 1 year ago

Yes I had made the "AddFile" calls, but my package version is 1.0.0

VitaliyMF commented 1 year ago

Yes I had made the "AddFile" calls, but my package version is 1.0.0

You need to reference the last version of NReco.Logging.File which is 1.1.6.

I cannot reproduce that in "TwoLogFilesMvc" example. I've added this to LoggingDemoController:

        public IActionResult LogMessage(string logLevel, string msg) {
            throw new Exception("TEST");
            Logger.Log( (LogLevel)Enum.Parse(typeof(LogLevel), logLevel, true), msg);
            return Ok();
        }

and in the app_debug.log I see

2023-02-17T11:17:38.1676606+02:00   FAIL    [Microsoft.AspNetCore.Server.Kestrel]   [ApplicationError]  Connection id "0HMOGOV763NPR", Request id "0HMOGOV763NPR:00000003": An unhandled exception was thrown by the application.System.Exception: TEST
ShaunStanley commented 1 year ago

Hi,

Sorry I made a small mistake. For the version referenced, it is 1.1.6. I had mistaken the version of my project and the added reference.

VitaliyMF commented 1 year ago

If you can to reproduce the issue with an example code please let me know your steps. Most likely something wrong with your app's configuration (I don't have your code and cannot say what is wrong).

ShaunStanley commented 1 year ago

Sure thing, maybe let me try out first, then if the issue still persist, I will send over the example code.

lukasf commented 1 year ago

I think the problem is with your code @ShaunStanley. You catch and handle the exception (no re-throw) and you do not log yourself. So there won't be an UnhandledException and there won't be any log entry. If you handle an exception yourself and you also want it to be logged, you need to log it yourself in the catch handler.