Skip to content

Commit

Permalink
Merge pull request #8 from jrsearles/fix/psql-cast-error
Browse files Browse the repository at this point in the history
fix: resolve long to int cast error
  • Loading branch information
jrsearles authored Jan 13, 2024
2 parents 150c10d + 4f1a307 commit 2109476
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="MinVer" Version="4.3.0" />
<PackageVersion Include="MySql.Data" Version="8.0.22" />
<PackageVersion Include="Npgsql" Version="5.0.1.1" />
<PackageVersion Include="Npgsql" Version="8.0.1" />
<PackageVersion Include="NSubstitute" Version="4.4.0" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="6.0.1" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
Expand Down
8 changes: 4 additions & 4 deletions src/Elmah.AspNetCore.MsSql/SqlErrorLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Elmah.AspNetCore.MsSql;
public class SqlErrorLog : ErrorLog
{
private const int MaxAppNameLength = 60;
private volatile bool _tableCreationChecked = false;
private volatile bool _checkTableExists = false;

/// <summary>
/// Initializes a new instance of the <see cref="SqlErrorLog" /> class
Expand Down Expand Up @@ -53,7 +53,7 @@ public SqlErrorLog(string connectionString, string? schemaName, string? tableNam
DatabaseSchemaName = !string.IsNullOrWhiteSpace(schemaName) ? schemaName! : "dbo";
DatabaseTableName = !string.IsNullOrWhiteSpace(tableName) ? tableName! : "ELMAH_Error";

_tableCreationChecked = !createTablesIfNotExist;
_checkTableExists = createTablesIfNotExist;
}

/// <summary>
Expand Down Expand Up @@ -164,12 +164,12 @@ public override async Task<int> GetErrorsAsync(ErrorLogFilterCollection filters,
/// </summary>
private async Task EnsureTablesExistAsync(CancellationToken cancellationToken)
{
if (_tableCreationChecked)
if (!_checkTableExists)
{
return;
}

_tableCreationChecked = true;
_checkTableExists = false;

using var connection = new SqlConnection(ConnectionString);
await connection.OpenAsync(cancellationToken);
Expand Down
101 changes: 52 additions & 49 deletions src/Elmah.AspNetCore.Postgresql/PgsqlErrorLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Elmah.AspNetCore.Postgresql;
public class PgsqlErrorLog : ErrorLog
{
private const int MaxAppNameLength = 60;
private volatile bool _checkTableExists = false;

/// <summary>
/// Initializes a new instance of the <see cref="PgsqlErrorLog" /> class
Expand All @@ -38,10 +39,7 @@ public PgsqlErrorLog(string connectionString, bool createTablesIfNotExist)

ConnectionString = connectionString;

if (createTablesIfNotExist)
{
CreateTableIfNotExists();
}
_checkTableExists = createTablesIfNotExist;
}

/// <summary>
Expand All @@ -60,24 +58,27 @@ public PgsqlErrorLog(string connectionString, bool createTablesIfNotExist)
public override async Task LogAsync(Error error, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(error);

await this.EnsureTableExistsAsync(cancellationToken);

var errorXml = ErrorXml.EncodeString(error);

using (var connection = new NpgsqlConnection(ConnectionString))
using (var command = Commands.LogError(error.Id, ApplicationName, error.HostName, error.Type, error.Source,
error.Message, error.User, error.StatusCode, error.Time, errorXml))
{
command.Connection = connection;
await connection.OpenAsync(cancellationToken);
await command.ExecuteNonQueryAsync(cancellationToken);
}
using var connection = new NpgsqlConnection(ConnectionString);
using var command = Commands.LogError(error.Id, ApplicationName, error.HostName, error.Type, error.Source,
error.Message, error.User, error.StatusCode, error.Time, errorXml);
command.Connection = connection;

await connection.OpenAsync(cancellationToken);
await command.ExecuteNonQueryAsync(cancellationToken);
}

public override async Task<ErrorLogEntry?> GetErrorAsync(Guid id, CancellationToken cancellationToken)
{
await this.EnsureTableExistsAsync(cancellationToken);

string? errorXml;

using (var connection = new NpgsqlConnection(ConnectionString))
using var connection = new NpgsqlConnection(ConnectionString);
using (var command = Commands.GetErrorXml(ApplicationName, id))
{
command.Connection = connection;
Expand Down Expand Up @@ -107,56 +108,58 @@ public override async Task<int> GetErrorsAsync(ErrorLogFilterCollection filters,
throw new ArgumentOutOfRangeException(nameof(pageSize), pageSize, null);
}

using (var connection = new NpgsqlConnection(ConnectionString))
await this.EnsureTableExistsAsync(cancellationToken);

using var connection = new NpgsqlConnection(ConnectionString);
await connection.OpenAsync(cancellationToken);

using (var command = Commands.GetErrorsXml(ApplicationName, errorIndex, pageSize))
{
await connection.OpenAsync(cancellationToken);
command.Connection = connection;

using (var command = Commands.GetErrorsXml(ApplicationName, errorIndex, pageSize))
using var reader = await command.ExecuteReaderAsync(cancellationToken);
while (await reader.ReadAsync(cancellationToken))
{
command.Connection = connection;

using var reader = await command.ExecuteReaderAsync(cancellationToken);
while (await reader.ReadAsync(cancellationToken))
{
var id = reader.GetGuid(0);
var xml = reader.GetString(1);
var error = ErrorXml.DecodeString(id, xml);
errorEntryList.Add(new ErrorLogEntry(this, error));
}
var id = reader.GetGuid(0);
var xml = reader.GetString(1);
var error = ErrorXml.DecodeString(id, xml);
errorEntryList.Add(new ErrorLogEntry(this, error));
}
}

using (var command = Commands.GetErrorsXmlTotal(ApplicationName))
{
command.Connection = connection;
return (int)(await command.ExecuteScalarAsync(cancellationToken))!;
}
using (var command = Commands.GetErrorsXmlTotal(ApplicationName))
{
command.Connection = connection;
return Convert.ToInt32(await command.ExecuteScalarAsync(cancellationToken));
}
}

/// <summary>
/// Creates the necessary tables and sequences used by this implementation
/// </summary>
private void CreateTableIfNotExists()
private async Task EnsureTableExistsAsync(CancellationToken cancellationToken)
{
using (var connection = new NpgsqlConnection(ConnectionString))
if (!_checkTableExists)
{
connection.Open();
return;
}

using (var cmdCheck = Commands.CheckTable())
{
cmdCheck.Connection = connection;
// ReSharper disable once PossibleNullReferenceException
var exists = (bool) cmdCheck.ExecuteScalar()!;

if (!exists)
{
using (var cmdCreate = Commands.CreateTable())
{
cmdCreate.Connection = connection;
cmdCreate.ExecuteNonQuery();
}
}
}
_checkTableExists = false;

using var connection = new NpgsqlConnection(ConnectionString);
await connection.OpenAsync(cancellationToken);

using var cmdCheck = Commands.CheckTable();
cmdCheck.Connection = connection;

// ReSharper disable once PossibleNullReferenceException
var exists = (bool)(await cmdCheck.ExecuteScalarAsync(cancellationToken))!;

if (!exists)
{
using var cmdCreate = Commands.CreateTable();
cmdCreate.Connection = connection;
await cmdCreate.ExecuteNonQueryAsync(cancellationToken);
}
}

Expand Down

0 comments on commit 2109476

Please sign in to comment.