Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
f2calv committed Sep 16, 2024
1 parent 36f866d commit 75eb2da
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions samples/GenericHost/Services/MyBackgroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,33 @@ public MyBackgroundService(ILogger<MyBackgroundService> logger, IHostApplication

protected async override Task ExecuteAsync(CancellationToken cancellationToken)
{
_logger.LogDebug($"starting {nameof(ExecuteAsync)}...");
_logger.LogInformation("{serviceName} starting {methodName}...", nameof(MyBackgroundService), nameof(ExecuteAsync));

//log-in
if (!await _googlePhotosSvc.LoginAsync()) throw new GooglePhotosException($"login failed!");
if (!await _googlePhotosSvc.LoginAsync(cancellationToken)) throw new GooglePhotosException($"login failed!");

//get existing/create new album
var albumTitle = $"{DateTime.UtcNow:yyyy-MM-dd HH:mm:ss}-{Guid.NewGuid()}";//make-up a random title
var album = await _googlePhotosSvc.GetOrCreateAlbumAsync(albumTitle) ?? throw new GooglePhotosException("album creation failed!");
Console.WriteLine($"{nameof(album)} '{album.title}' id is '{album.id}'");
_logger.LogInformation("{serviceName} {name} '{title}' id is '{id}'", nameof(MyBackgroundService), nameof(album), album.title, album.id);

//upload single media item and assign to album
var mediaItem = await _googlePhotosSvc.UploadSingle($"{_testFolder}test1.jpg", album.id) ?? throw new GooglePhotosException("media item upload failed!");
Console.WriteLine($"{nameof(mediaItem)} '{mediaItem.mediaItem.filename}' id is '{mediaItem.mediaItem.id}'");
var path = $"{_testFolder}test1.jpg";
var mediaItem = await _googlePhotosSvc.UploadSingle(path, album.id) ?? throw new GooglePhotosException($"media item '{path}' upload failed!");
_logger.LogInformation("{serviceName} {name} '{filename}' id is '{id}'",
nameof(MyBackgroundService), nameof(mediaItem), mediaItem.mediaItem.filename, mediaItem.mediaItem.id);

//retrieve all media items in the album
var albumMediaItems = await _googlePhotosSvc.GetMediaItemsByAlbumAsync(album.id, cancellationToken: cancellationToken).ToListAsync(cancellationToken) ?? throw new GooglePhotosException("retrieve media items by album id failed!");
var i = 1;
foreach (var item in albumMediaItems)
{
Console.WriteLine($"{i}\t{item.filename}\t{item.mediaMetadata.width}x{item.mediaMetadata.height}");
_logger.LogInformation("{serviceName} album #{i} {filename} {width}x{height}", nameof(MyBackgroundService), i, item.filename,
item.mediaMetadata.width, item.mediaMetadata.height);
i++;
}

_logger.LogDebug($"exiting {nameof(ExecuteAsync)}...");
_logger.LogInformation("{serviceName} exiting {methodName}...", nameof(MyBackgroundService), nameof(ExecuteAsync));
_appLifetime.StopApplication();
}
}

0 comments on commit 75eb2da

Please sign in to comment.