mirror of
https://github.com/temporal-community/temporal-ai-agent.git
synced 2026-03-15 14:08:08 +01:00
Added logger factory to worker and logging to activities (#26)
Signed-off-by: Keith Tenzer <ktenzer@keiths-mbp.lan> Co-authored-by: Keith Tenzer <ktenzer@keiths-mbp.lan>
This commit is contained in:
@@ -2,6 +2,7 @@ using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using Temporalio.Activities;
|
||||
using TrainSearchWorker.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace TrainSearchWorker.Activities;
|
||||
|
||||
@@ -23,6 +24,7 @@ public class TrainActivities
|
||||
[Activity]
|
||||
public async Task<JourneyResponse> SearchTrains(SearchTrainsRequest request)
|
||||
{
|
||||
ActivityExecutionContext.Current.Logger.LogInformation($"SearchTrains from {request.From} to {request.To}");
|
||||
var response = await _client.GetAsync(
|
||||
$"api/search?from={Uri.EscapeDataString(request.From)}" +
|
||||
$"&to={Uri.EscapeDataString(request.To)}" +
|
||||
@@ -30,17 +32,21 @@ public class TrainActivities
|
||||
$"&return_time={Uri.EscapeDataString(request.ReturnTime)}");
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
|
||||
// Deserialize into JourneyResponse rather than List<Journey>
|
||||
var journeyResponse = await response.Content.ReadFromJsonAsync<JourneyResponse>(_jsonOptions)
|
||||
?? throw new InvalidOperationException("Received null response from API");
|
||||
|
||||
ActivityExecutionContext.Current.Logger.LogInformation("SearchTrains completed");
|
||||
|
||||
return journeyResponse;
|
||||
}
|
||||
|
||||
[Activity]
|
||||
public async Task<BookTrainsResponse> BookTrains(BookTrainsRequest request)
|
||||
{
|
||||
ActivityExecutionContext.Current.Logger.LogInformation($"Booking trains with IDs: {request.TrainIds}");
|
||||
|
||||
// Build the URL using the train IDs from the request
|
||||
var url = $"api/book/{Uri.EscapeDataString(request.TrainIds)}";
|
||||
|
||||
@@ -52,6 +58,8 @@ public class TrainActivities
|
||||
var bookingResponse = await response.Content.ReadFromJsonAsync<BookTrainsResponse>(_jsonOptions)
|
||||
?? throw new InvalidOperationException("Received null response from API");
|
||||
|
||||
ActivityExecutionContext.Current.Logger.LogInformation("BookTrains completed");
|
||||
|
||||
return bookingResponse;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,19 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using Temporalio.Client;
|
||||
using Temporalio.Worker;
|
||||
using TrainSearchWorker.Activities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Console;
|
||||
|
||||
// Set up dependency injection
|
||||
var services = new ServiceCollection();
|
||||
|
||||
var loggerFactory = LoggerFactory.Create(builder =>
|
||||
{
|
||||
builder
|
||||
.AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] ")
|
||||
.SetMinimumLevel(LogLevel.Information);
|
||||
});
|
||||
|
||||
// Add HTTP client
|
||||
services.AddHttpClient("TrainApi", client =>
|
||||
{
|
||||
@@ -31,7 +40,10 @@ Console.WriteLine($"Connecting to Temporal at address: {address}");
|
||||
Console.WriteLine($"Using namespace: {ns}");
|
||||
|
||||
// Create worker options
|
||||
var options = new TemporalWorkerOptions("agent-task-queue-legacy");
|
||||
var options = new TemporalWorkerOptions("agent-task-queue-legacy")
|
||||
{
|
||||
LoggerFactory = loggerFactory
|
||||
};
|
||||
|
||||
// Register activities
|
||||
var activities = serviceProvider.GetRequiredService<TrainActivities>();
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.4" />
|
||||
<PackageReference Include="Temporalio" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user