Skip to content

Evolution and Logging #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions BotMessageRouting/BotMessageRouting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bot.Builder" Version="4.*" />
<PackageReference Include="WindowsAzure.Storage" Version="9.*" />
<PackageReference Include="Microsoft.Bot.Builder" Version="4.7.2" />
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the wildcards

<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ public static CloudTable GetTable(string connectionString, string tableName)
return cloudTableClient?.GetTableReference(tableName);
}

/// <summary>
/// Tries to insert the given entry into the given table.
/// </summary>
/// <typeparam name="T">TableEntity derivative.</typeparam>
/// <param name="cloudTable">The destination table.</param>
/// <param name="entryToInsert">The entry to insert into the table.</param>
/// <returns>True, if the given entry was inserted successfully. False otherwise.</returns>
public static async Task<bool> ReplaceAsync<T>(CloudTable cloudTable, T entryToInsert) where T : ITableEntity
{
TableOperation replaceOperation = TableOperation.InsertOrReplace(entryToInsert);
TableResult replaceResult = null;

try
{
replaceResult = await cloudTable.ExecuteAsync(replaceOperation);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine($"Failed to replace the given entity into table '{cloudTable.Name}': {e.Message}");
return false;
}

return (replaceResult?.Result != null);
}

/// <summary>
/// Tries to insert the given entry into the given table.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Underscore.Bot.MessageRouting.Logging;
using Underscore.Bot.MessageRouting.Models;
using Underscore.Bot.MessageRouting.Models.Azure;

Expand All @@ -24,7 +25,7 @@ public class AzureTableRoutingDataStore : IRoutingDataStore
protected const string TableNameAggregationChannels = "AggregationChannels";
protected const string TableNameConnectionRequests = "ConnectionRequests";
protected const string TableNameConnections = "Connections";

protected readonly ILogger _logger;
protected CloudTable _botInstancesTable;
protected CloudTable _usersTable;
protected CloudTable _aggregationChannelsTable;
Expand All @@ -35,13 +36,15 @@ public class AzureTableRoutingDataStore : IRoutingDataStore
/// Constructor.
/// </summary>
/// <param name="connectionString">The connection string associated with an Azure Table Storage.</param>
public AzureTableRoutingDataStore(string connectionString)
public AzureTableRoutingDataStore(string connectionString, ILogger logger)
{
if (string.IsNullOrEmpty(connectionString))
{
throw new ArgumentNullException("The connection string cannot be null or empty");
}

_logger = logger;

_botInstancesTable = AzureStorageHelper.GetTable(connectionString, TableNameBotInstances);
_usersTable = AzureStorageHelper.GetTable(connectionString, TableNameUsers);
_aggregationChannelsTable = AzureStorageHelper.GetTable(connectionString, TableNameAggregationChannels);
Expand Down Expand Up @@ -216,11 +219,11 @@ protected virtual async void MakeSureTablesExistAsync()
try
{
await cloudTable.CreateIfNotExistsAsync();
Debug.WriteLine($"Table '{cloudTable.Name}' created or did already exist");
_logger.LogInformation($"Table '{cloudTable.Name}' created or did already exist");
}
catch (StorageException e)
{
Debug.WriteLine($"Failed to create table '{cloudTable.Name}' (perhaps it already exists): {e.Message}");
_logger.LogError($"Failed to create table '{cloudTable.Name}' (perhaps it already exists): {e.Message}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,11 @@ public virtual bool IsAssociatedWithAggregation(ConversationReference conversati

return (conversationReference != null
&& aggregationParties != null
&& aggregationParties.Count() > 0
&& aggregationParties.Count > 0
&& aggregationParties.Where(aggregationChannel =>
aggregationChannel.Conversation.Id == conversationReference.Conversation.Id
&& aggregationChannel.ServiceUrl == conversationReference.ServiceUrl
&& aggregationChannel.ChannelId == conversationReference.ChannelId).Count() > 0);
aggregationChannel.Conversation.Id.Equals(conversationReference.Conversation.Id, StringComparison.OrdinalIgnoreCase)
&& aggregationChannel.ServiceUrl.Equals(conversationReference.ServiceUrl)
&& aggregationChannel.ChannelId.Equals(conversationReference.ChannelId)).Any());
}

/// <summary>
Expand Down
44 changes: 44 additions & 0 deletions BotMessageRouting/MessageRouting/Logging/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;

namespace Underscore.Bot.MessageRouting.Logging
{
public class ConsoleLogger : Bot.MessageRouting.Logging.ILogger
{
private readonly Microsoft.Extensions.Logging.ILogger _logger;

public ConsoleLogger(Microsoft.Extensions.Logging.ILogger logger)
{
_logger = logger;
}

public void Log(string message, [CallerMemberName] string methodName = "")
{
_logger.LogDebug(BuildMessage(message, methodName));
}

public void LogError(string message, [CallerMemberName] string methodName = "")
{
_logger.LogError(BuildMessage(message, methodName));
}

public void LogInformation(string message, [CallerMemberName] string methodName = "")
{
_logger.LogInformation(BuildMessage(message, methodName));
}

private string BuildMessage(string message, string methodName)
{
var msg = $"{DateTime.Now}> ";
if (!string.IsNullOrWhiteSpace(methodName))
msg += $"{methodName}: {message}";
else
msg += message;

return msg;
}
}
}
10 changes: 10 additions & 0 deletions BotMessageRouting/MessageRouting/Logging/DebugLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,15 @@ public void Log(string message, [CallerMemberName] string methodName = "")

Debug.WriteLine($"{DateTime.Now}> {message}");
}

public void LogError(string message, [CallerMemberName] string methodName = "")
{
Log(message, methodName);
}

public void LogInformation(string message, [CallerMemberName] string methodName = "")
{
Log(message, methodName);
}
}
}
15 changes: 15 additions & 0 deletions BotMessageRouting/MessageRouting/Logging/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,20 @@ public interface ILogger
/// <param name="message">The message to log.</param>
/// <param name="methodName">Resolved by the [CallerMemberName] attribute. No value required.</param>
void Log(string message, [CallerMemberName] string methodName = "");

/// <summary>
/// Logs the given message.
/// </summary>
/// <param name="message">The message to log.</param>
/// <param name="methodName">Resolved by the [CallerMemberName] attribute. No value required.</param>
void LogInformation(string message, [CallerMemberName] string methodName = "");

/// <summary>
/// Logs the given message.
/// </summary>
/// <param name="message">The message to log.</param>
/// <param name="methodName">Resolved by the [CallerMemberName] attribute. No value required.</param>
void LogError(string message, [CallerMemberName] string methodName = "");

}
}