Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try Catch does not log #51

Open
ShaunStanley opened this issue Feb 17, 2023 · 9 comments
Open

Try Catch does not log #51

ShaunStanley opened this issue Feb 17, 2023 · 9 comments
Labels

Comments

@ShaunStanley
Copy link

ShaunStanley commented Feb 17, 2023

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

ShaunStanley commented Feb 17, 2023

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

@VitaliyMF
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

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

@lukasf
Copy link

lukasf commented Mar 13, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants