internal static void CreateSqliteTarget(bool callFromOutside = true)
        {
            try
            {
                NLog.Config.LoggingConfiguration config = new NLog.Config.LoggingConfiguration();
                NLog.Targets.Target           dbTarget  = FindTargetByName(TARGETDATABASE);
                SqliteConnectionStringBuilder connectionStringBuilder = new SqliteConnectionStringBuilder {
                    ConnectionString = GetConnectionString()
                };
                string dbFileName = connectionStringBuilder.DataSource;
                if (!string.IsNullOrEmpty(dbFileName))
                {
                    FileInfo fileInfo = new FileInfo(dbFileName);
                    if (!fileInfo.Directory.Exists)
                    {
                        fileInfo.Directory.Create();
                    }
                    if (!File.Exists(dbFileName))
                    {
                        using (FileStream fs = File.Create(dbFileName))
                        {
                            fs.Close();
                        }

                        config.AddTarget(dbTarget);
                        using (var context = new NLog.Config.InstallationContext(Console.Out))//Console.Out
                        {
                            config.Install(context);
                        }
                    }
                    if (callFromOutside)
                    {
                        //register Logs db file
                        using (FileSystemWatcher fileWatcher = new FileSystemWatcher(fileInfo.Directory.FullName, fileInfo.Name))
                        {
                            fileWatcher.NotifyFilter          = NotifyFilters.FileName;
                            fileWatcher.EnableRaisingEvents   = true;
                            fileWatcher.IncludeSubdirectories = false;

                            fileWatcher.Renamed += (sender, e) =>
                            {
                                CreateSqliteTarget(false);
                            };
                            fileWatcher.Deleted += (sender, e) =>
                            {
                                CreateSqliteTarget(false);
                            };
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                KraftLogger.LogError(ex);
            }
        }
Beispiel #2
0
 public static void RestartApplication(IApplicationLifetime applicationLifetime, FileSystemEventArgs e)
 {
     try {
         if (applicationLifetime != null)
         {
             applicationLifetime.StopApplication();
             KraftLogger.LogDebug($"Method: RestartApplication(KraftLoggerExtensions): Stopping application caused by {e.ChangeType} file {e.FullPath}");
             if (!applicationLifetime.ApplicationStopping.IsCancellationRequested)
             {
                 Task.Delay(10 * 1000, applicationLifetime.ApplicationStopping);
             }
         }
         else
         {
             KraftLogger.LogDebug("Method: RestartApplication(nlog): applicationLifetime is null.");
         }
     }
     catch (Exception exception)
     {
         KraftLogger.LogError(exception, "Method: RestartApplication(nlog)(IApplicationLifetime applicationLifetime)");
     }
 }
        public static object GetData(string connectionString, Dictionary <string, object> args)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new Exception("Missing connection string. Please check your configuration and try again.");
            }

            var databaseTarget = (NLog.Targets.DatabaseTarget)FindTargetByName(TARGETDATABASE);

            string dbGetDataCommand = NLog.LogManager.Configuration.Variables["SelectWithFilter"].Text;

            List <Dictionary <string, object> > results = new List <Dictionary <string, object> >();

            using (DbConnection connection = new SqliteConnection(connectionString))
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }

                using (DbCommand command = connection.CreateCommand())
                {
                    try
                    {
                        Dictionary <string, object> cmdParams = GetPagedParameters(args);

                        ParametrizeCommand(cmdParams, command);
                        command.CommandText = GeneratePagedStatement(dbGetDataCommand);

                        using (DbDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    var result = new Dictionary <string, object>(reader.FieldCount);
                                    for (int i = 0; i < reader.FieldCount; i++)
                                    {
                                        string fldname = reader.GetName(i);
                                        if (fldname == null)
                                        {
                                            continue;
                                        }
                                        fldname = fldname.Trim();
                                        if (fldname.Length == 0)
                                        {
                                            throw new Exception($"Empty name when reading the output of a query. The field index is {i}. The query is: {command.CommandText}");
                                        }
                                        if (result.ContainsKey(fldname))
                                        {
                                            throw new Exception($"Duplicated field name in the output of a query. The field is: {fldname} , the query is: {command.CommandText}");
                                        }
                                        object v = reader.GetValue(i);
                                        result.Add(fldname, (v is DBNull) ? null : v);
                                    }
                                    results.Add(result);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        KraftLogger.LogError(ex);
                    }
                }
            }
            return(results);
        }