This commit is contained in:
Wolfgang Hottgenroth 2021-12-10 17:56:55 +01:00
parent a07d97fc9b
commit aca1e946d8
2 changed files with 29 additions and 14 deletions

View File

@ -44,7 +44,7 @@ namespace com.krohne.genericdatabaseapiservice.Services {
public DbInfoService(IConfiguration configuration, ILogger<DbInfoService> logger) {
Configuration = configuration;
Logger = Logger;
Logger = logger;
Console.WriteLine("Database Infofile: {0}", Configuration["Database:InfoFile"]);
DbInfos = JsonConvert.DeserializeObject<Dictionary<string, DbInfoObject>>(File.ReadAllText(Configuration["Database:InfoFile"]));
}
@ -93,8 +93,8 @@ namespace com.krohne.genericdatabaseapiservice.Services {
var databaseConnInfo = DbInfoService.GetInfoStringByTag(databaseTag);
Logger.LogDebug("ConnInfo: {0}", databaseConnInfo);
Logger.LogDebug("Statement: {0}", selectStatement);
Logger.LogInformation("ConnInfo: {0}", databaseConnInfo);
Logger.LogInformation("Statement: {0}", selectStatement);
using (var conn = new MySqlConnection(databaseConnInfo)) {
await conn.OpenAsync();
@ -103,48 +103,48 @@ namespace com.krohne.genericdatabaseapiservice.Services {
cmd.CommandText = selectStatement;
if (input != null){
foreach (var propertyInfo in typeof(TIN).GetProperties()) {
Logger.LogDebug("Input Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
Logger.LogInformation("Input Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
var attributes = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true);
var dma = (DataMemberAttribute)attributes[0];
Logger.LogDebug("Input DataMember name: {0} {1} ", dma.Name, dma.TypeId);
Logger.LogInformation("Input DataMember name: {0} {1} ", dma.Name, dma.TypeId);
var value = propertyInfo.GetValue(input);
Logger.LogDebug("Input Value: {0}", value);
Logger.LogInformation("Input Value: {0}", value);
cmd.Parameters.AddWithValue(dma.Name, propertyInfo.GetValue(input));
}
} else {
Logger.LogDebug("no input data");
Logger.LogInformation("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()) {
Logger.LogDebug("Output Property name: {0} {1} ", propertyInfo.Name, propertyInfo.PropertyType);
Logger.LogInformation("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);
Logger.LogDebug("Output DataMember name: {0} {1} {2} ", dma.Name, dma.TypeId, ordinal);
Logger.LogInformation("Output DataMember name: {0} {1} {2} ", dma.Name, dma.TypeId, ordinal);
if (await reader.IsDBNullAsync(ordinal)) {
propertyInfo.SetValue(item, null);
Logger.LogDebug("Output Value: null");
Logger.LogInformation("Output Value: null");
} else if (propertyInfo.PropertyType == typeof(System.String)) {
var value = reader.GetString(ordinal);
propertyInfo.SetValue(item, value);
Logger.LogDebug("Output Value:{0}", value);
Logger.LogInformation("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);
Logger.LogDebug("Output Value:{0}", (System.Int32)value);
Logger.LogInformation("Output Value:{0}", (System.Int32)value);
} else if (propertyInfo.PropertyType == typeof(System.DateTime)) {
var value = reader.GetDateTime(ordinal);
propertyInfo.SetValue(item, value);
Logger.LogDebug("Output Value:{0}", value);
Logger.LogInformation("Output Value:{0}", value);
} else {
throw new UnsupportedDataTypeException();
}
}
itemList.Add(item);
Logger.LogDebug("Item is {0}", item);
Logger.LogInformation("Item is {0}", item);
}
}
}

15
appsettings.json-example Normal file
View File

@ -0,0 +1,15 @@
{
"Logging": {
"Console": {
"FormatterName": "simple",
"FormatterOptions": {
"TimestampFormat": "[yyyy-MM-dd HH:mm:ss] ",
"SingleLine": true
}
},
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*"
}