logging using microsoft.extensions.logging
This commit is contained in:
37
DbService.cs
37
DbService.cs
@ -6,6 +6,7 @@ using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MySqlConnector;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@ -38,10 +39,12 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
|
||||
public class DbInfoService : IDbInfoService {
|
||||
private readonly IConfiguration Configuration;
|
||||
private readonly ILogger<DbInfoService> Logger;
|
||||
private Dictionary<string, DbInfoObject> DbInfos;
|
||||
|
||||
public DbInfoService(IConfiguration configuration) {
|
||||
public DbInfoService(IConfiguration configuration, ILogger<DbInfoService> logger) {
|
||||
Configuration = configuration;
|
||||
Logger = Logger;
|
||||
Console.WriteLine("Database Infofile: {0}", Configuration["Database:InfoFile"]);
|
||||
DbInfos = JsonConvert.DeserializeObject<Dictionary<string, DbInfoObject>>(File.ReadAllText(Configuration["Database:InfoFile"]));
|
||||
}
|
||||
@ -76,10 +79,12 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
|
||||
public class DbService : IDbService {
|
||||
private readonly IConfiguration Configuration;
|
||||
private readonly ILogger<DbService> Logger;
|
||||
private readonly IDbInfoService DbInfoService;
|
||||
|
||||
public DbService(IConfiguration configuration, IDbInfoService dbInfoService) {
|
||||
public DbService(IConfiguration configuration, ILogger<DbService> logger, IDbInfoService dbInfoService) {
|
||||
Configuration = configuration;
|
||||
Logger = logger;
|
||||
DbInfoService = dbInfoService;
|
||||
}
|
||||
|
||||
@ -88,8 +93,8 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
|
||||
var databaseConnInfo = DbInfoService.GetInfoStringByTag(databaseTag);
|
||||
|
||||
Console.WriteLine("ConnInfo: {0}", databaseConnInfo);
|
||||
Console.WriteLine("Statement: {0}", selectStatement);
|
||||
Logger.LogDebug("ConnInfo: {0}", databaseConnInfo);
|
||||
Logger.LogDebug("Statement: {0}", selectStatement);
|
||||
|
||||
using (var conn = new MySqlConnection(databaseConnInfo)) {
|
||||
await conn.OpenAsync();
|
||||
@ -98,57 +103,59 @@ namespace com.krohne.genericdatabaseapiservice.Services {
|
||||
cmd.CommandText = selectStatement;
|
||||
if (input != null){
|
||||
foreach (var propertyInfo in typeof(TIN).GetProperties()) {
|
||||
Console.WriteLine("Input Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
|
||||
Logger.LogDebug("Input Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
|
||||
var attributes = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true);
|
||||
var dma = (DataMemberAttribute)attributes[0];
|
||||
Console.WriteLine("Input DataMember name: {0} {1} ", dma.Name, dma.TypeId);
|
||||
Logger.LogDebug("Input DataMember name: {0} {1} ", dma.Name, dma.TypeId);
|
||||
var value = propertyInfo.GetValue(input);
|
||||
Console.WriteLine("Input Value: {0}", value);
|
||||
Logger.LogDebug("Input Value: {0}", value);
|
||||
cmd.Parameters.AddWithValue(dma.Name, propertyInfo.GetValue(input));
|
||||
}
|
||||
} else {
|
||||
Console.WriteLine("no input data");
|
||||
Logger.LogDebug("no input data");
|
||||
}
|
||||
using (var reader = await cmd.ExecuteReaderAsync()) {
|
||||
while (await reader.ReadAsync()) {
|
||||
var item = Activator.CreateInstance<TOUT>();
|
||||
foreach (var propertyInfo in typeof(TOUT).GetProperties()) {
|
||||
Console.WriteLine("Output Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
|
||||
Logger.LogDebug("Output Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
|
||||
var attributes = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true);
|
||||
var dma = (DataMemberAttribute)attributes[0];
|
||||
int ordinal = reader.GetOrdinal(dma.Name);
|
||||
Console.WriteLine("Output DataMember name: {0} {1} {2} ", dma.Name, dma.TypeId, ordinal);
|
||||
Logger.LogDebug("Output DataMember name: {0} {1} {2} ", dma.Name, dma.TypeId, ordinal);
|
||||
if (await reader.IsDBNullAsync(ordinal)) {
|
||||
propertyInfo.SetValue(item, null);
|
||||
Console.WriteLine("Output Value: null");
|
||||
Logger.LogDebug("Output Value: null");
|
||||
} else if (propertyInfo.PropertyType == typeof(System.String)) {
|
||||
var value = reader.GetString(ordinal);
|
||||
propertyInfo.SetValue(item, value);
|
||||
Console.WriteLine("Output Value:{0}", value);
|
||||
Logger.LogDebug("Output Value:{0}", value);
|
||||
} else if (propertyInfo.PropertyType == typeof(System.Int32) ||
|
||||
propertyInfo.PropertyType == typeof(System.Nullable<System.Int32>)) {
|
||||
var value = reader.GetInt32(ordinal);
|
||||
propertyInfo.SetValue(item, value);
|
||||
Console.WriteLine("Output Value:{0}", (System.Int32)value);
|
||||
Logger.LogDebug("Output Value:{0}", (System.Int32)value);
|
||||
} else if (propertyInfo.PropertyType == typeof(System.DateTime)) {
|
||||
var value = reader.GetDateTime(ordinal);
|
||||
propertyInfo.SetValue(item, value);
|
||||
Console.WriteLine("Output Value:{0}", value);
|
||||
Logger.LogDebug("Output Value:{0}", value);
|
||||
} else {
|
||||
throw new UnsupportedDataTypeException();
|
||||
}
|
||||
}
|
||||
itemList.Add(item);
|
||||
Console.WriteLine("Item is {0}", item);
|
||||
Logger.LogDebug("Item is {0}", item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (itemList.Count == 0) {
|
||||
Logger.LogWarning("no data found");
|
||||
throw new NoDataFoundException();
|
||||
}
|
||||
if (justOne && itemList.Count > 1) {
|
||||
Logger.LogWarning("too much data found");
|
||||
throw new TooMuchDataFoundException();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user