示例#1
0
        public static void Main(string[] args)
        {
            var logConnectionString = ConnectionStringBuilder.BuildSQLConnectionString(Configuration.GetValue <string>("DATABASE_SERVER"), Configuration.GetValue <string>("DATABASE"), Configuration.GetValue <string>("CIVILAPP_USERNAME"), Configuration.GetValue <string>("CIVILAPP_PASSWORD"));
            var colOpts             = new ColumnOptions();

            colOpts.Store.Remove(StandardColumn.Properties);
            colOpts.Store.Add(StandardColumn.LogEvent);
            colOpts.AdditionalColumns = new List <SqlColumn>
            {
                new SqlColumn()
                {
                    ColumnName = "Source", DataType = System.Data.SqlDbType.VarChar, PropertyName = "SourceContext", AllowNull = false, DataLength = 250
                }
            };

            Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));

            Log.Logger = new LoggerConfiguration()
                         .ReadFrom.Configuration(Configuration)
                         .Enrich.FromLogContext()
                         .Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
                                                      .WithDefaultDestructurers()
                                                      .WithDestructurers(new[] { new SqlExceptionDestructurer() }))
                         .WriteTo.Debug()
                         .WriteTo.Console(
                outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}")
                         .WriteTo.MSSqlServer(
                connectionString: logConnectionString,
                sinkOptions: new MSSqlServerSinkOptions()
            {
                SchemaName         = "Logging",
                TableName          = "CivilAppLogs",
                AutoCreateSqlTable = true
            },
                columnOptions: colOpts
                )
                         .CreateLogger();

            try
            {
                Log.Information("Application starting");

                CreateHostBuilder(args).Build().Run();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
        public async Task Delete(string cmd)
        {
            using (var sqlConnection =
                       new SqlConnection(ConnectionStringBuilder.BuildSQLConnectionString(_sqlSettings)))
            {
                await sqlConnection.OpenAsync();

                using (var sqlCmd = new SqlCommand(cmd, sqlConnection))
                {
                    sqlCmd.ExecuteNonQuery();
                }
            }
        }
示例#3
0
        public async Task Insert(string cmd)
        {
            try
            {
                using (var sqlConnection =
                           new SqlConnection(ConnectionStringBuilder.BuildSQLConnectionString(_sqlSettings)))
                {
                    await sqlConnection.OpenAsync();

                    using (var sqlCmd = new SqlCommand(cmd, sqlConnection))
                    {
                        sqlCmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                var s = 2;
            }
        }
示例#4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "CivilApp.API", Version = "v1"
                });
            });

            services.AddScoped(typeof(IAppLogger <>), typeof(LogAdapter <>));
            services.AddScoped(typeof(IAsyncRepository <>), typeof(EfRepository <>));
            services.AddScoped <IDateTimeService, DateTimeService>();
            services.AddScoped <IJacketSequenceService, JacketSequenceService>();
            services.AddDbContext <JacketContext>(options =>
            {
                options.UseSqlServer(ConnectionStringBuilder.BuildSQLConnectionString(
                                         Configuration.GetValue <string>("DATABASE_SERVER"), Configuration.GetValue <string>("DATABASE"),
                                         Configuration.GetValue <string>("CIVILAPP_USERNAME"),
                                         Configuration.GetValue <string>("CIVILAPP_PASSWORD")));
            });
        }
        public async Task <List <T> > Get <T>(string cmd)
        {
            var objList = new List <T>();

            using (var sqlConnection = new SqlConnection(ConnectionStringBuilder.BuildSQLConnectionString(_sqlSettings)))
            {
                await sqlConnection.OpenAsync();

                using (var sqlCmd = new SqlCommand(cmd, sqlConnection))
                {
                    var reader = await sqlCmd.ExecuteReaderAsync();

                    var schema = reader.GetSchemaTable();

                    string[] columnNames = new string[schema.Rows.Count];

                    for (int i = 0; i < schema.Rows.Count; i++)
                    {
                        columnNames[i] = schema.Rows[i][0].ToString();
                    }

                    while (await reader.ReadAsync())
                    {
                        var obj   = Activator.CreateInstance <T>();
                        var props = obj.GetType().GetProperties();
                        foreach (var columnName in columnNames)
                        {
                            foreach (var prop in props)
                            {
                                if (columnName.ToUpper() == prop.Name.ToUpper())
                                {
                                    // To handle null value
                                    if (reader[columnName] == null)
                                    {
                                        continue;
                                    }

                                    if (prop.PropertyType == typeof(Guid))
                                    {
                                        var result = Guid.Empty;
                                        // To Check NULL GUID
                                        if (Guid.TryParse(reader[columnName].ToString(), out result))
                                        {
                                            prop.SetValue(obj, result);
                                        }
                                        break;
                                    }
                                    else if (prop.PropertyType == typeof(DateTime))
                                    {
                                        prop.SetValue(obj, Convert.ToDateTime(reader[columnName].ToString()));
                                        break;
                                    }
                                    else if (prop.PropertyType == typeof(Int32))
                                    {
                                        prop.SetValue(obj, Convert.ToInt32(reader[columnName].ToString()));
                                        break;
                                    }
                                    else if (prop.PropertyType == typeof(Int16))        // For ChoreTypeId
                                    {
                                        prop.SetValue(obj, Convert.ToInt16(reader[columnName].ToString()));
                                        break;
                                    }
                                    else if (prop.PropertyType == typeof(TimeSpan))
                                    {
                                        prop.SetValue(obj, TimeSpan.Parse(reader[columnName].ToString()));
                                        break;
                                    }
                                    else if (prop.PropertyType == typeof(Decimal))
                                    {
                                        prop.SetValue(obj, Convert.ToDecimal(reader[columnName].ToString()));
                                        break;
                                    }
                                    else
                                    {
                                        prop.SetValue(obj, reader[columnName].ToString());
                                        break;
                                    }
                                }
                            }
                        }

                        objList.Add(obj);
                    }
                }

                return(objList);
            }
        }