Skip to content

Commit

Permalink
ImapSync: improve logger massages in ImapSyncService.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvgolikov committed Sep 23, 2022
1 parent f1e9b17 commit 555c4d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 84 deletions.
52 changes: 19 additions & 33 deletions Services/ASC.Mail.ImapSync/ImapSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ImapSyncService(
_log = loggerProvider.CreateLogger("ASC.Mail.ImapSyncService");
_logProvider = loggerProvider;

_log.InfoImapSyncServiceReady();
_log.InfoImapSyncService("created");
}
catch (Exception ex)
{
Expand All @@ -68,22 +68,26 @@ public ImapSyncService(

public Task RedisSubscribe(CancellationToken cancellationToken)
{
if (_redisClient == null) return StopAsync(cancellationToken);
if (_redisClient == null)
{
_log.CritImapSyncServiceConstruct("Don't connect to Redis.");

return StopAsync(cancellationToken);
}
try
{
return _redisClient.SubscribeQueueKey<ASC.Mail.ImapSync.Models.RedisCachePubSubItem<CachedTenantUserMailBox>>(CreateNewClient);
var result= _redisClient.SubscribeQueueKey<ASC.Mail.ImapSync.Models.RedisCachePubSubItem<CachedTenantUserMailBox>>(CreateNewClient);

_log.InfoImapSyncService("subscrube to Redis chanel");

return result;
}
catch (Exception ex)
{
_log.ErrorImapSyncServiceSubscribeRedis(ex.Message);
_log.CritImapSyncServiceConstruct($"Don't subscribe to Redis chanel.\n {ex.Message}");

return StopAsync(cancellationToken);
}
finally
{
_log.InfoImapSyncServiceTrySubscribeRedis();
}
}

public async Task CreateNewClient(ASC.Mail.ImapSync.Models.RedisCachePubSubItem<CachedTenantUserMailBox> redisCachePubSubItem)
Expand Down Expand Up @@ -112,7 +116,7 @@ public async Task CreateNewClient(ASC.Mail.ImapSync.Models.RedisCachePubSubItem<
{
if (!clients.TryAdd(cashedTenantUserMailBox.UserName, null))
{
_log.DebugImapSyncServiceTryCreateClient(cashedTenantUserMailBox.UserName, cashedTenantUserMailBox.Folder);
_log.InfoImapSyncService($"create new client for {cashedTenantUserMailBox.UserName}");

return;
}
Expand Down Expand Up @@ -142,7 +146,7 @@ public async Task CreateNewClient(ASC.Mail.ImapSync.Models.RedisCachePubSubItem<

await ClearUserRedis(cashedTenantUserMailBox.UserName);

_log.ErrorImapSyncServiceStop($"Create mail client for user {cashedTenantUserMailBox.UserName}. {ex}");
_log.ErrorImapSyncCreateClient($"user {cashedTenantUserMailBox.UserName}. {ex}");
}
}
}
Expand All @@ -158,48 +162,30 @@ private void Client_DeleteClient(object sender, EventArgs e)
trashValue.OnCriticalError -= Client_DeleteClient;
trashValue?.Stop();

_log.InfoImapSyncServiceClientDiedAndWasRemove(clientKey);
_log.InfoImapSyncService($"delete client {clientKey}");
}
else
{
_log.InfoImapSyncServiceClientDiedAndWasntRemove(clientKey);
_log.InfoImapSyncService($"client {clientKey} died but don`t remove.");
}
}
}

public Task StartAsync(CancellationToken cancellationToken)
{
try
{
_log.InfoImapSyncServiceStart();

return RedisSubscribe(cancellationToken);
}
catch (Exception ex)
{
_log.ErrorImapSyncService(ex.Message);

return StopAsync(cancellationToken);
}
}
public Task StartAsync(CancellationToken cancellationToken) => RedisSubscribe(cancellationToken);

public Task StopAsync(CancellationToken cancellationToken)
{
try
{
_cancelTokenSource.Cancel();

_log.InfoImapSyncServiceStoping();
_log.InfoImapSyncService("stopping");

}
catch (Exception ex)
{
_log.ErrorImapSyncServiceStop(ex.ToString());
}
finally
{
_log.InfoImapSyncServiceStop();
}

return Task.CompletedTask;
}
Expand All @@ -210,7 +196,7 @@ public async Task<int> ClearUserRedis(string UserName)

string RedisKey = "ASC.MailAction:" + UserName;

var localRedisClient= _redisFactory.GetRedisClient();
var localRedisClient = _redisFactory.GetRedisClient();

if (localRedisClient == null) return 0;

Expand Down
59 changes: 8 additions & 51 deletions Services/ASC.Mail.ImapSync/Loggers/ImapSyncServiceLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,20 @@

internal static partial class ImapSyncServiceLogger
{
[LoggerMessage(Level = LogLevel.Information, Message = "Service is ready")]
public static partial void InfoImapSyncServiceReady(this ILogger logger);
[LoggerMessage(EventId = 100, Level = LogLevel.Information, Message = "Service is {serviceText}.")]
public static partial void InfoImapSyncService(this ILogger logger, string serviceText);

[LoggerMessage(Level = LogLevel.Critical, Message = "ImapSyncService error under construct: {error}")]
[LoggerMessage(EventId = 1, Level = LogLevel.Critical, Message = "ImapSyncService error under construct: {error}")]
public static partial void CritImapSyncServiceConstruct(this ILogger logger, string error);

[LoggerMessage(Level = LogLevel.Error, Message = "Didn`t subscribe to redis. Message: {error}")]
public static partial void ErrorImapSyncServiceSubscribeRedis(this ILogger logger, string error);

[LoggerMessage(Level = LogLevel.Information, Message = "Try to subscribe redis...")]
public static partial void InfoImapSyncServiceTrySubscribeRedis(this ILogger logger);

[LoggerMessage(Level = LogLevel.Debug, Message = "Online Users count: {count}")]
[LoggerMessage(EventId = 11, Level = LogLevel.Debug, Message = "Online Users count: {count}")]
public static partial void DebugImapSyncServiceOnlineUsersCount(this ILogger logger, int count);

[LoggerMessage(Level = LogLevel.Debug, Message = "User Activity -> {username}, folder={folder}. Wait for client start...")]
[LoggerMessage(EventId = 12, Level = LogLevel.Debug, Message = "User Activity -> {username}, folder={folder}. Wait for client start...")]
public static partial void DebugImapSyncServiceWaitForClient(this ILogger logger, string username, int folder);

[LoggerMessage(Level = LogLevel.Debug, Message = "User Activity -> {username}, folder = {folder}. Try to create client ...")]
public static partial void DebugImapSyncServiceTryCreateClient(this ILogger logger, string username, int folder);

[LoggerMessage(Level = LogLevel.Information, Message = "Can`t create Mail client for user {username}.")]
public static partial void InfoImapSyncServiceCantCreateMailClient(this ILogger logger, string username);

[LoggerMessage(Level = LogLevel.Warning, Message = "[TIMEOUT] Create mail client for user {username}. {error}")]
public static partial void WarnImapSyncServiceCreateClientTimeout(this ILogger logger, string username, string error);

[LoggerMessage(Level = LogLevel.Information, Message = "[CANCEL] Create mail client for user {userName}.")]
public static partial void InfoImapSyncServiceCreateClientCancel(this ILogger logger, string username);

[LoggerMessage(Level = LogLevel.Error, Message = "[AuthenticationException] Create mail client for user {username}. {error}")]
public static partial void ErrorImapSyncServiceCreateClientAuth(this ILogger logger, string username, string error);

[LoggerMessage(Level = LogLevel.Error, Message = "[WebException] Create mail client for user {username}. {error}")]
public static partial void ErrorImapSyncServiceCreateClientWeb(this ILogger logger, string username, string error);

[LoggerMessage(Level = LogLevel.Error, Message = "Create mail client for user {username}. {error}")]
public static partial void ErrorImapSyncServiceCreateClientException(this ILogger logger, string username, string error);

[LoggerMessage(Level = LogLevel.Information, Message = "ImapSyncService. MailImapClient {clientKey} died and was remove.")]
public static partial void InfoImapSyncServiceClientDiedAndWasRemove(this ILogger logger, string clientKey);

[LoggerMessage(Level = LogLevel.Information, Message = "ImapSyncService. MailImapClient {clientKey} died, bud wasn`t remove.")]
public static partial void InfoImapSyncServiceClientDiedAndWasntRemove(this ILogger logger, string clientKey);

[LoggerMessage(Level = LogLevel.Information, Message = "Start service\r\n")]
public static partial void InfoImapSyncServiceStart(this ILogger logger);

[LoggerMessage(Level = LogLevel.Error, Message = "{error}")]
public static partial void ErrorImapSyncService(this ILogger logger, string error);

[LoggerMessage(Level = LogLevel.Information, Message = "Stoping service\r\n")]
public static partial void InfoImapSyncServiceStoping(this ILogger logger);

[LoggerMessage(Level = LogLevel.Error, Message = "Stop service Error: {error}\r\n")]
[LoggerMessage(EventId = 2, Level = LogLevel.Error, Message = "Stop service Error: {error}\r\n")]
public static partial void ErrorImapSyncServiceStop(this ILogger logger, string error);

[LoggerMessage(Level = LogLevel.Information, Message = "Stop service\r\n")]
public static partial void InfoImapSyncServiceStop(this ILogger logger);
[LoggerMessage(EventId = 2, Level = LogLevel.Error, Message = "Create client Error: {error}\r\n")]
public static partial void ErrorImapSyncCreateClient(this ILogger logger, string error);
}

0 comments on commit 555c4d8

Please sign in to comment.