示例#1
0
        public static SeqForwarderConfig CreateDefaultConfig(StoragePathFeature storagePath)
        {
            if (!Directory.Exists(storagePath.StorageRootPath))
            {
                Directory.CreateDirectory(storagePath.StorageRootPath);
            }

            var config = new SeqForwarderConfig
            {
                Output =
                {
                    ServerUrl            = "http://localhost:5341",
                    ApiKey               = null,
                    EventBodyLimitBytes  =              256 * 1024,
                    RawPayloadLimitBytes = 10 * 1024 * 1024
                },
                Diagnostics =
                {
                    InternalLoggingLevel = LogEventLevel.Information,
                    InternalLogPath      = GetDefaultInternalLogPath()
                },
                Storage =
                {
                    BufferSizeBytes = 64 * 1024 * 1024
                }
            };

            SeqForwarderConfig.Write(storagePath.ConfigFilePath, config);

            return(config);
        }
 static void List(TextWriter cout, SeqForwarderConfig config)
 {
     foreach (var pr in ReadPairs(config))
     {
         cout.WriteLine($"{pr.Key}:");
         cout.WriteLine($"  {pr.Value}");
     }
 }
示例#3
0
        static ActiveLogBufferMap CreateActiveLogBufferMap(TempFolder tmp)
        {
            var config = new SeqForwarderConfig();
            var map    = new ActiveLogBufferMap(tmp.Path, config.Storage, config.Output, new InertLogShipperFactory());

            map.Load();
            return(map);
        }
示例#4
0
        // Re-create just in case it was inadvertently deleted.
        SeqForwarderConfig LoadOrCreateConfig()
        {
            if (File.Exists(_storagePath.ConfigFilePath))
            {
                return(SeqForwarderConfig.Read(_storagePath.ConfigFilePath));
            }

            return(InstallCommand.CreateDefaultConfig(_storagePath));
        }
示例#5
0
        protected override int Run(TextWriter cout)
        {
            Log.Logger = new LoggerConfiguration()
                         .WriteTo.LiterateConsole()
                         .CreateLogger();

            try
            {
                if (string.IsNullOrWhiteSpace(_file))
                {
                    Log.Fatal("A log file to import must be specified");
                    return(1);
                }

                if (!File.Exists(_file))
                {
                    Log.Fatal("The specified import log file does not exist");
                    return(1);
                }

                string serverUrl = null, apiKey = null;
                if (_serverInformation.IsUrlSpecified)
                {
                    serverUrl = _serverInformation.Url;
                    apiKey    = _serverInformation.ApiKey;
                }
                else if (File.Exists(_storagePath.ConfigFilePath))
                {
                    var config = SeqForwarderConfig.Read(_storagePath.ConfigFilePath);
                    if (string.IsNullOrEmpty(config.Output.ServerUrl))
                    {
                        serverUrl = config.Output.ServerUrl;
                        apiKey    = _serverInformation.IsApiKeySpecified ? _serverInformation.ApiKey : config.Output.ApiKey;
                    }
                }

                if (serverUrl == null)
                {
                    Log.Fatal("A Seq server URL must be specified or set in the SeqForwarder.json config file");
                    return(1);
                }

                Task.Run(async() => await Run(serverUrl, apiKey, _file, _keyValueProperties.Properties, 256 * 1024, 1024 * 1024)).Wait();
                return(0);
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Could not complete import");
                return(1);
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
示例#6
0
 public SeqForwarderModule(string bufferPath, SeqForwarderConfig config)
 {
     if (bufferPath == null)
     {
         throw new ArgumentNullException(nameof(bufferPath));
     }
     if (config == null)
     {
         throw new ArgumentNullException(nameof(config));
     }
     _bufferPath = bufferPath;
     _config     = config;
 }
        public static SeqForwarderConfig CreateDefaultConfig(StoragePathFeature storagePath)
        {
            if (!Directory.Exists(storagePath.StorageRootPath))
            {
                Directory.CreateDirectory(storagePath.StorageRootPath);
            }

            var config = new SeqForwarderConfig();

            SeqForwarderConfig.Write(storagePath.ConfigFilePath, config);

            return(config);
        }
示例#8
0
        protected override int Run(TextWriter cout)
        {
            try
            {
                var config = SeqForwarderConfig.Read(_storagePath.ConfigFilePath);
                var buffer = new LogBuffer(_storagePath.BufferPath, config.Storage.BufferSizeBytes);
                buffer.Truncate();
                return(0);
            }
            catch (Exception ex)
            {
                var logger = new LoggerConfiguration().WriteTo.LiterateConsole().CreateLogger();

                logger.Fatal(ex, "Could not truncate log buffer");
                return(1);
            }
        }
        static void Set(SeqForwarderConfig config, string key, string value)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var steps = key.Split('.');

            if (steps.Length != 2)
            {
                throw new ArgumentException("The format of the key is incorrect; run the command without any arguments to view all keys.");
            }

            var first = config.GetType().GetTypeInfo().DeclaredProperties
                        .Where(p => p.GetMethod.IsPublic && !p.GetMethod.IsStatic)
                        .SingleOrDefault(p => Camelize(p.Name) == steps[0]);

            if (first == null)
            {
                throw new ArgumentException("The key could not be found; run the command without any arguments to view all keys.");
            }

            var v = first.GetValue(config);

            var second = v.GetType().GetTypeInfo().DeclaredProperties
                         .Where(p => p.GetMethod.IsPublic && p.SetMethod.IsPublic && !p.GetMethod.IsStatic)
                         .SingleOrDefault(p => Camelize(p.Name) == steps[1]);

            if (second == null)
            {
                throw new ArgumentException("The key could not be found; run the command without any arguments to view all keys.");
            }

            var targetValue = Convert.ChangeType(value, second.PropertyType);

            second.SetValue(v, targetValue);
        }
示例#10
0
        protected override int Run(TextWriter cout)
        {
            try
            {
                var config = SeqForwarderConfig.Read(_storagePath.ConfigFilePath);
                var buffer = new LogBuffer(_storagePath.BufferPath, config.Storage.BufferSizeBytes);
                buffer.Enumerate((k, v) =>
                {
                    var s = Encoding.UTF8.GetString(v);
                    Console.WriteLine(s);
                });
                return(0);
            }
            catch (Exception ex)
            {
                var logger = new LoggerConfiguration().WriteTo.LiterateConsole().CreateLogger();

                logger.Fatal(ex, "Could not dump events");
                return(1);
            }
        }
示例#11
0
        protected override int Run(TextWriter cout)
        {
            try
            {
                var config = SeqForwarderConfig.Read(_storagePath.ConfigFilePath);

                if (_key != null)
                {
                    if (_clear)
                    {
                        Clear(config, _key);
                        SeqForwarderConfig.Write(_storagePath.ConfigFilePath, config);
                    }
                    else if (_value != null)
                    {
                        Set(config, _key, _value);
                        SeqForwarderConfig.Write(_storagePath.ConfigFilePath, config);
                    }
                    else
                    {
                        Print(cout, config, _key);
                    }
                }
                else
                {
                    List(cout, config);
                }

                return(0);
            }
            catch (Exception ex)
            {
                var logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();

                logger.Fatal(ex, "Could not update config");
                return(1);
            }
        }
示例#12
0
        static void Print(TextWriter cout, SeqForwarderConfig config, string key)
        {
            if (cout == null)
            {
                throw new ArgumentNullException(nameof(cout));
            }
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var pr = ReadPairs(config).SingleOrDefault(p => p.Key == key);

            if (pr.Key == null)
            {
                throw new ArgumentException($"Option {key} not found");
            }
            cout.WriteLine(pr.Value);
        }
示例#13
0
        protected override int Run(TextWriter cout)
        {
            try
            {
                var config = SeqForwarderConfig.Read(_storagePath.ConfigFilePath);
                using (var buffer = new ActiveLogBufferMap(_storagePath.BufferPath, config.Storage, config.Output, new InertLogShipperFactory()))
                {
                    buffer.Load();
                    buffer.Enumerate((k, v) =>
                    {
                        var s = Encoding.UTF8.GetString(v);
                        Console.WriteLine(s);
                    });
                }
                return(0);
            }
            catch (Exception ex)
            {
                var logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();

                logger.Fatal(ex, "Could not dump events");
                return(1);
            }
        }
示例#14
0
        void Install(TextWriter cout)
        {
            cout.WriteLine("Installing service...");

            if (PathIsNetworkPath(_storagePath.StorageRootPath))
            {
                throw new ArgumentException("Seq Forwarder requires a local (or SAN) storage location; network shares are not supported.");
            }

            SeqForwarderConfig config;

            if (File.Exists(_storagePath.ConfigFilePath))
            {
                cout.WriteLine($"Using the configuration found in {_storagePath.ConfigFilePath}...");
                config = SeqForwarderConfig.Read(_storagePath.ConfigFilePath);
            }
            else
            {
                cout.WriteLine($"Creating a new configuration file in {_storagePath.ConfigFilePath}...");
                config = CreateDefaultConfig(_storagePath);
            }

            var args = new List <string>
            {
                "/LogFile=\"\"",
                "/ShowCallStack",
                "/storage=\"" + _storagePath.StorageRootPath + "\"",
                GetType().Assembly.Location
            };

            if (_serviceCredentials.IsUsernameSpecified)
            {
                if (!_serviceCredentials.IsPasswordSpecified)
                {
                    throw new ArgumentException("If a service user account is specified, a password for the account must also be specified.");
                }

                // https://technet.microsoft.com/en-us/library/cc794944(v=ws.10).aspx
                cout.WriteLine($"Ensuring {_serviceCredentials.Username} is granted 'Log on as a Service' rights...");
                AccountRightsHelper.EnsureServiceLogOnRights(_serviceCredentials.Username);

                cout.WriteLine($"Granting {_serviceCredentials.Username} rights to {_storagePath.StorageRootPath}...");
                GiveFullControl(_storagePath.StorageRootPath, _serviceCredentials.Username);

                cout.WriteLine($"Granting {_serviceCredentials.Username} rights to {config.Diagnostics.InternalLogPath}...");
                GiveFullControl(config.Diagnostics.InternalLogPath, _serviceCredentials.Username);

                var listenUri = MakeListenUriReservationPattern();
                cout.WriteLine($"Adding URL reservation at {listenUri} for {_serviceCredentials.Username} (may request UAC elevation)...");
                NetSh.AddUrlAcl(listenUri, _serviceCredentials.Username);

                args.Insert(0, "/username=\"" + _serviceCredentials.Username + "\"");
                args.Insert(0, "/password=\"" + _serviceCredentials.Password + "\"");
            }
            else
            {
                cout.WriteLine($"Granting the NT AUTHORITY\\LocalService account rights to {_storagePath.StorageRootPath}...");
                GiveFullControl(_storagePath.StorageRootPath, "NT AUTHORITY\\LocalService");

                cout.WriteLine($"Granting NT AUTHORITY\\LocalService account rights to {config.Diagnostics.InternalLogPath}...");
                GiveFullControl(config.Diagnostics.InternalLogPath, "NT AUTHORITY\\LocalService");

                var listenUri = MakeListenUriReservationPattern();
                cout.WriteLine($"Adding URL reservation at {listenUri} for the Local Service account (may request UAC elevation)...");
                NetSh.AddUrlAcl(listenUri, "NT AUTHORITY\\LocalService");
            }

            ManagedInstallerClass.InstallHelper(args.ToArray());

            Console.ForegroundColor = ConsoleColor.Green;
            cout.WriteLine("Service installed successfully.");
            Console.ResetColor();
        }
示例#15
0
 static void Clear(SeqForwarderConfig config, string key)
 {
     Set(config, key, null);
 }
 public SeqForwarderModule(string bufferPath, string listenUri, SeqForwarderConfig config)
 {
     _bufferPath = bufferPath ?? throw new ArgumentNullException(nameof(bufferPath));
     _config     = config ?? throw new ArgumentNullException(nameof(config));
     _listenUri  = listenUri ?? throw new ArgumentNullException(nameof(listenUri));
 }