-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S6423: Always log failures in Azure Functions (#960)
- Loading branch information
1 parent
75a02d8
commit 26706ef
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"title": "Azure Functions should log all failures.", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"error-handling" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-6423", | ||
"sqKey": "S6423", | ||
"scope": "Main", | ||
"defaultQualityProfiles": [ ], | ||
"quickfix": "unknown" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
Capturing and logging errors is critical to monitoring the health of your Azure Functions application. | ||
|
||
Each `catch` block inside an Azure Function should log helpful details about the failure. Moreover, the logging should not be done at `Debug` or `Trace` level. | ||
|
||
Consider using the built-in integration with Application Insights for better monitoring of your Application. | ||
|
||
// If you want to factorize the description uncomment the following line and create the file. | ||
//include::../description.adoc[] | ||
|
||
== Noncompliant Code Example | ||
|
||
[source,csharp] | ||
---- | ||
[FunctionName("Foo")] | ||
public static async Task<IActionResult> Run( | ||
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, | ||
ILogger log) | ||
{ | ||
try | ||
{ | ||
// do stuff that can fail | ||
} | ||
catch (Exception ex) | ||
{ | ||
// the failure is not logged at all OR is logged at DEBUG/TRACE level | ||
} | ||
} | ||
---- | ||
|
||
== Compliant Solution | ||
|
||
[source,csharp] | ||
---- | ||
[FunctionName("Foo")] | ||
public static async Task<IActionResult> Run( | ||
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, | ||
ILogger log) | ||
{ | ||
try | ||
{ | ||
// do stuff that can fail | ||
} | ||
catch (Exception ex) | ||
{ | ||
log.LogError("Give details that will help investigations", ex); | ||
} | ||
} | ||
---- | ||
|
||
== See | ||
|
||
* https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-error-pages?tabs=csharp[Azure Functions error handling and retries] | ||
* https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring[Monitor Azure Functions] | ||
* https://docs.microsoft.com/en-us/azure/azure-monitor/app/azure-functions-supported-features[Application Insights for Azure Functions supported features] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |