Skip to content

Commit 5eef7e4

Browse files
committed
Trigger Files
1 parent 0ce7bac commit 5eef7e4

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

articles/azure-functions/functions-bindings-azure-mysql-trigger.md

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,29 @@ The MySQL trigger binds to a `IReadOnlyList<MySqlChange<T>>`, a list of `MySqlCh
121121
The following example shows a [C# function](functions-dotnet-class-library.md) that is invoked when there are changes to the `Product` table:
122122

123123
```cs
124-
using System.Collections.Generic;
125124
using Microsoft.Azure.Functions.Worker;
126125
using Microsoft.Azure.Functions.Worker.Extensions.MySql;
127126
using Microsoft.Extensions.Logging;
128-
using Newtonsoft.Json;
127+
using AzureMySqlSamples.Common;
129128

130129
namespace AzureMySqlSamples.TriggerBindingSamples
131130
{
132-
public static class ProductsTrigger
133-
{
134-
[FunctionName(nameof(ProductsTrigger))]
131+
private static readonly Action<ILogger, string, Exception> _loggerMessage = LoggerMessage.Define<string>(LogLevel.Information, eventId: new EventId(0, "INFO"), formatString: "{Message}");
132+
133+
[Function(nameof(ProductsTrigger))]
135134
public static void Run(
136135
[MySqlTrigger("Products", "MySqlConnectionString")]
137-
IReadOnlyList<MySqlChange<Product>> changes,
138-
ILogger logger)
136+
IReadOnlyList<MySqlChange<Product>> changes, FunctionContext context)
139137
{
138+
ILogger logger = context.GetLogger("ProductsTrigger");
140139
// The output is used to inspect the trigger binding parameter in test methods.
141-
logger.LogInformation("MySQL Changes: " + JsonConvert.SerializeObject(changes));
140+
foreach (MySqlChange<Product> change in changes)
141+
{
142+
Product product = change.Item;
143+
_loggerMessage(logger, $"Change operation: {change.Operation}", null);
144+
_loggerMessage(logger, $"Product Id: {product.ProductId}, Name: {product.Name}, Cost: {product.Cost}", null);
145+
}
142146
}
143-
}
144147
}
145148
```
146149

@@ -205,8 +208,8 @@ The following example shows a [C# function](functions-dotnet-class-library.md) t
205208
using System.Collections.Generic;
206209
using Microsoft.Azure.WebJobs;
207210
using Microsoft.Azure.WebJobs.Extensions.MySql;
208-
using Microsoft.Extensions.Logging;
209-
using Newtonsoft.Json;
211+
using Microsoft.Extensions.Logging;
212+
using AzureMySqlSamples.Common;
210213

211214
namespace AzureMySqlSamples.TriggerBindingSamples
212215
{
@@ -219,7 +222,12 @@ namespace AzureMySqlSamples.TriggerBindingSamples
219222
ILogger logger)
220223
{
221224
// The output is used to inspect the trigger binding parameter in test methods.
222-
logger.LogInformation("MySQL Changes: " + JsonConvert.SerializeObject(changes));
225+
foreach (MySqlChange<Product> change in changes)
226+
{
227+
Product product = change.Item;
228+
logger.LogInformation($"Change operation: {change.Operation}");
229+
logger.LogInformation($"Product Id: {product.ProductId}, Name: {product.Name}, Cost: {product.Cost}");
230+
}
223231
}
224232
}
225233
}
@@ -539,21 +547,20 @@ The following is sample python code for the function_app.py file:
539547

540548
```python
541549
import json
542-
550+
import logging
543551
import azure.functions as func
544-
545-
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
546-
547-
@app.generic_trigger(arg_name="changes", type="mysqlTrigger",
548-
table_name="Products",
549-
connection_string_setting="AzureWebJobsMySqlConnectionString")
550-
@app.generic_output_binding(arg_name="r", type="mysql",
551-
command_text="Products1",
552-
connection_string_setting="AzureWebJobsMySqlConnectionString")
553-
def mysql_trigger(changes, r: func.Out[func.MySqlRow]) -> str:
554-
row = func.MySqlRow.from_dict(json.loads(changes)[0]["Item"])
555-
r.set(row)
556-
return "OK"
552+
553+
app = func.FunctionApp()
554+
555+
# The function gets triggered when a change (Insert, Update)
556+
# is made to the Products table.
557+
@app.function_name(name="ProductsTrigger")
558+
@app.mysql_trigger(arg_name="products",
559+
table_name="Products",
560+
connection_string_setting="MySqlConnectionString")
561+
562+
def products_trigger(products: str) -> None:
563+
logging.info("MySQL Changes: %s", json.loads(products))
557564
```
558565

559566
# [v1](#tab/python-v1)

0 commit comments

Comments
 (0)