Skip to content

Commit

Permalink
switched to ilogger on server
Browse files Browse the repository at this point in the history
  • Loading branch information
freever committed Sep 13, 2024
1 parent 135fcb2 commit c4e0627
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
3 changes: 1 addition & 2 deletions src/Blauhaus.Push.Server/Blauhaus.Push.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
<Compile Include=".Ioc\ServiceCollectionExtensions.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Blauhaus.Analytics.Console" Version="2.1.1" />
<ItemGroup>
<PackageReference Include="Blauhaus.Common.Utils" Version="2.4.3" />
<PackageReference Include="Microsoft.Azure.NotificationHubs" Version="4.2.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Blauhaus.Push.Server.Extensions
{
public static class DeviceRegistrationExtensions
{
public static bool IsNotValid(this IDeviceRegistration? deviceRegistration, object sender, IAnalyticsLogger logger, out Error error)
public static bool IsNotValid(this IDeviceRegistration? deviceRegistration, object sender, ILogger logger, out Error error)
{

if (deviceRegistration == null)
Expand Down
38 changes: 16 additions & 22 deletions src/Blauhaus.Push.Server/Service/PushNotificationsServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ namespace Blauhaus.Push.Server.Service
{
public class PushNotificationsServerService : IPushNotificationsServerService
{
private readonly IAnalyticsLogger<PushNotificationsServerService> _logger;
private readonly ILogger<PushNotificationsServerService> _logger;
private readonly INotificationHubClientProxy _hubClientProxy;

public PushNotificationsServerService(
IAnalyticsLogger<PushNotificationsServerService> logger,
ILogger<PushNotificationsServerService> logger,
INotificationHubClientProxy hubClientProxy)
{
_logger = logger;
Expand All @@ -38,7 +38,7 @@ public async Task<Response<IDeviceRegistration>> UpdateDeviceRegistrationAsync(I
return Response.Failure<IDeviceRegistration>(validationError);
}

_logger.BeginTimedScope(LogLevel.Information, "Register device {DeviceIdentifier} for push notifications on {Platform}", deviceRegistration.DeviceIdentifier, deviceRegistration.Platform.Value);
_logger.LogInformation("Start to register device {DeviceIdentifier} for push notifications on {Platform}", deviceRegistration.DeviceIdentifier, deviceRegistration.Platform.Value);

_hubClientProxy.Initialize(hub);

Expand Down Expand Up @@ -73,13 +73,13 @@ public async Task<Response<IDeviceRegistration>> UpdateDeviceRegistrationAsync(I

public async Task<Response<IDeviceRegistration>> LoadRegistrationForUserDeviceAsync(string userId, string deviceIdentifier, IPushNotificationsHub hub)
{
using var _ = _logger.BeginTimedScope(LogLevel.Debug, "Load push notification registration for user {UserId} on device {DeviceIdentifier}", userId, deviceIdentifier);
_logger.LogInformation("Attempt to load push notification registration for user {UserId} on device {DeviceIdentifier}", userId, deviceIdentifier);

_hubClientProxy.Initialize(hub);

var installationId = userId + "___" + deviceIdentifier;
string installationId = userId + "___" + deviceIdentifier;

var installationExists = await _hubClientProxy.InstallationExistsAsync(installationId);
bool installationExists = await _hubClientProxy.InstallationExistsAsync(installationId);

if (!installationExists)
{
Expand All @@ -104,31 +104,27 @@ public async Task<Response<IDeviceRegistration>> LoadRegistrationForUserDeviceAs
return Response.Success<IDeviceRegistration>(deviceRegistration);
}

public Task SendNotificationToUserAsync(IPushNotification notification, string userId, IPushNotificationsHub hub)
{
using var _ = _logger.BeginTimedScope(LogLevel.Information, "Send push notification {PushNoticiationName} to user {UserId}", notification.Name, userId);
_logger.LogTrace("Push Notification: {@PushNotification}", notification);

public async Task SendNotificationToUserAsync(IPushNotification notification, string userId, IPushNotificationsHub hub)
{
var tags = new List<string>
{
$"(UserId_{userId} && {notification.Name})"
};

return SendNotificationToTagsAsync(notification, hub, tags);
await SendNotificationToTagsAsync(notification, hub, tags);
_logger.LogInformation("Sent push notification {PushNoticiationName} to user {UserId}", notification.Name, userId);
}

public Task SendNotificationToUserDeviceAsync(IPushNotification notification, string userId, string deviceIdentifier, IPushNotificationsHub hub)
public async Task SendNotificationToUserDeviceAsync(IPushNotification notification, string userId, string deviceIdentifier, IPushNotificationsHub hub)
{

using var _ = _logger.BeginTimedScope(LogLevel.Information, "Send push notification {PushNoticiationName} to user {UserId} on device {DeviceIdentifier}", notification.Name, userId, deviceIdentifier);
_logger.LogTrace("Push Notification: {@PushNotification}", notification);


var tags = new List<string>
{
$"(UserId_{userId} && {notification.Name} && DeviceIdentifier_{deviceIdentifier})"
};

return SendNotificationToTagsAsync(notification, hub, tags);
await SendNotificationToTagsAsync(notification, hub, tags);
_logger.LogInformation("Sent push notification {PushNoticiationName} to user {UserId} on device {DeviceIdentifier}", notification.Name, userId, deviceIdentifier);

}

Expand Down Expand Up @@ -157,13 +153,11 @@ private async Task SendNotificationToTagsAsync(IPushNotification notification, I

public async Task<Response> DeregisterUserDeviceAsync(string userId, string deviceIdentifier, IPushNotificationsHub hub)
{
using var _ = _logger.BeginTimedScope(LogLevel.Information, "Deregister device {DeviceIdentifier} for user {UserId}", deviceIdentifier, userId);

_hubClientProxy.Initialize(hub);

var installationId = userId + "___" + deviceIdentifier;
string installationId = userId + "___" + deviceIdentifier;

var installationExists = await _hubClientProxy.InstallationExistsAsync(installationId);
bool installationExists = await _hubClientProxy.InstallationExistsAsync(installationId);
if (!installationExists)
{
_logger.LogWarning("No installation exists for user device, so there is nothing to deregister");
Expand Down

0 comments on commit c4e0627

Please sign in to comment.