Ejemplo n.º 1
0
        private static void WriteXmlConfigurationFile(LogAdministrator logAdministrator, string path)
        {
            try
            {
                logAdministrator
                .GetLogLevel(out var logLevel)
                .GetDestinationNames(out var destinationNames);

                var configurations = new Configurations()
                {
                    LogLevel     = logLevel,
                    Destinations = destinationNames
                                   .Select(name =>
                    {
                        logAdministrator
                        .GetDestinationConfiguration(name, out var configuration)
                        .GetCollectMiddlewareName(name, out var collectMiddlewareName)
                        .GetMiddlewareNames(name, out var middlewareNames);

                        return(new DestinationEntry
                        {
                            Name = name,
                            Parameters = configuration
                                         .Select(c => new Parameter {
                                Name = c.Key, Value = c.Value
                            })
                                         .ToList(),
                            CollectMiddleware = new ConfigurationEntry {
                                Name = collectMiddlewareName
                            },
                            Middlewares = middlewareNames
                                          .Select(n => new ConfigurationEntry {
                                Name = n
                            })
                                          .ToList(),
                        });
                    })
                                   .ToList()
                };

                using (var stringWriter = new CustomStringWriter(Encoding.UTF8))
                {
                    using (var xmlWriter = new XmlTextWriter(stringWriter)
                    {
                        Formatting = Formatting.Indented
                    })
                    {
                        var xmlSerializer = new XmlSerializer(typeof(Configurations));
                        xmlSerializer.Serialize(xmlWriter, configurations);
                        File.WriteAllText(path, stringWriter.ToString(), Encoding.UTF8);
                    }
                }
            }
            catch (Exception ex)
            {
                LogErrorHandler.Instance.Handle("Error writing xml configuration file", ex);
            }
        }