@@ -121,26 +121,29 @@ The MySQL trigger binds to a `IReadOnlyList<MySqlChange<T>>`, a list of `MySqlCh
121
121
The following example shows a [C# function](functions-dotnet-class-library.md) that is invoked when there are changes to the `Product` table:
122
122
123
123
```cs
124
- using System.Collections.Generic;
125
124
using Microsoft.Azure.Functions.Worker;
126
125
using Microsoft .Azure .Functions .Worker .Extensions .MySql ;
127
126
using Microsoft .Extensions .Logging ;
128
- using Newtonsoft . Json ;
127
+ using AzureMySqlSamples . Common ;
129
128
130
129
namespace AzureMySqlSamples .TriggerBindingSamples
131
130
{
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 ))]
135
134
public static void Run(
136
135
[MySqlTrigger (" Products" , " MySqlConnectionString" )]
137
- IReadOnlyList <MySqlChange <Product >> changes ,
138
- ILogger logger )
136
+ IReadOnlyList<MySqlChange<Product>> changes, FunctionContext context)
139
137
{
138
+ ILogger logger = context.GetLogger("ProductsTrigger");
140
139
// 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
+ }
142
146
}
143
- }
144
147
}
145
148
```
146
149
@@ -205,8 +208,8 @@ The following example shows a [C# function](functions-dotnet-class-library.md) t
205
208
using System.Collections.Generic;
206
209
using Microsoft .Azure .WebJobs ;
207
210
using Microsoft .Azure .WebJobs .Extensions .MySql ;
208
- using Microsoft.Extensions.Logging;
209
- using Newtonsoft.Json;
211
+ using Microsoft .Extensions .Logging ;
212
+ using AzureMySqlSamples . Common ;
210
213
211
214
namespace AzureMySqlSamples .TriggerBindingSamples
212
215
{
@@ -219,7 +222,12 @@ namespace AzureMySqlSamples.TriggerBindingSamples
219
222
ILogger logger )
220
223
{
221
224
// 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
+ }
223
231
}
224
232
}
225
233
}
@@ -539,21 +547,20 @@ The following is sample python code for the function_app.py file:
539
547
540
548
``` python
541
549
import json
542
-
550
+ import logging
543
551
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))
557
564
```
558
565
559
566
# [ v1] ( #tab/python-v1 )
0 commit comments