示例#1
0
        public ContainerBuilder WithFakeCommonProperties(int managedThreadId = 0, string machineName = null,
                                                         int processId       = 0, string processName = null, string threadName = null, string appDomainName = null)
        {
            _selfLogOutput = new StringWriter();
            SelfLog.Enable(TextWriter.Synchronized(_selfLogOutput));

            var mock = new Mock <ICommonProperties>();

            mock.SetupGet(m => m.ManagedThreadId).Returns(managedThreadId);
            mock.SetupGet(m => m.MachineName).Returns(machineName);
            mock.SetupGet(m => m.ProcessId).Returns(processId);
            mock.SetupGet(m => m.ProcessName).Returns(processName);
            mock.SetupGet(m => m.ThreadName).Returns(threadName);
            mock.SetupGet(m => m.AppDomainName).Returns(appDomainName);
            Container.RegisterInstance(mock.Object);
            return(this);
        }
示例#2
0
 public static IHostBuilder CreateHostBuilder(string[] args) =>
 Host.CreateDefaultBuilder(args)
 .UseSerilog((hostingContext, loggerConfiguration) =>
 {
     SelfLog.Enable(Console.WriteLine);
     // No DI here, so get option from Environment temporary.
     // see: https://github.com/serilog/serilog-aspnetcore/issues/169
     loggerConfiguration
     .ReadFrom.Configuration(hostingContext.Configuration)
     .Enrich.FromLogContext()
     //.Enrich.WithCorrelationIdHeader("TraceId")
     ;
 })
 .ConfigureWebHostDefaults(webBuilder =>
 {
     webBuilder.UseStartup <Startup>();
 });
 private static IPAddress ResolveIP(string uri)
 {
     try
     {
         var ipHostEntry = Dns.GetHostEntryAsync(uri).Result;
         if (!ipHostEntry.AddressList.Any())
         {
             return(null);
         }
         return(ipHostEntry.AddressList.First());
     }
     catch (Exception)
     {
         SelfLog.WriteLine("Could not resolve " + uri);
         return(null);
     }
 }
        private bool _disposedValue; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (_disposedValue)
            {
                return;
            }

            if (disposing)
            {
                FlushAndCloseEventHandlers();
                _semaphoreSlim.Dispose();

                SelfLog.WriteLine("Sink halted successfully.");
            }

            _disposedValue = true;
        }
示例#5
0
        private static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .WriteTo.Kafka(
                new JsonFormatter(), new KafkaOptions(new List <string> {
                "kafka:9092"
            }, "console-example"),
                new BatchOptions())
                         .Enrich.FromLogContext()
                         .CreateLogger();

            Log.Information("Hello World!");

            SelfLog.Enable(message => System.Console.WriteLine(message));

            Log.CloseAndFlush();
        }
        public static Batch Read(LogEventQueue queue, int batchPostingLimit, long batchSizeLimitBytes)
        {
            var batch = new Batch();
            var remainingBatchSizeBytes = batchSizeLimitBytes;

            while (true)
            {
                var result = queue.TryDequeue(remainingBatchSizeBytes, out var logEvent);
                if (result == LogEventQueue.DequeueResult.Ok)
                {
                    batch.LogEvents.Add(logEvent);
                    remainingBatchSizeBytes -= ByteSize.From(logEvent);

                    // Respect batch posting limit
                    if (batch.LogEvents.Count == batchPostingLimit)
                    {
                        batch.HasReachedLimit = true;
                        break;
                    }
                }
                else if (result == LogEventQueue.DequeueResult.MaxSizeViolation)
                {
                    if (batch.LogEvents.Count == 0)
                    {
                        // This single log event exceeds the batch size limit, let's drop it
                        queue.TryDequeue(long.MaxValue, out var logEventToDrop);

                        SelfLog.WriteLine(
                            "Event exceeds the batch size limit of {0} bytes set for this sink and will be dropped; data: {1}",
                            batchSizeLimitBytes,
                            logEventToDrop);
                    }
                    else
                    {
                        batch.HasReachedLimit = true;
                        break;
                    }
                }
                else if (result == LogEventQueue.DequeueResult.QueueEmpty)
                {
                    break;
                }
            }

            return(batch);
        }
        private async Task PostData(string signature, string dateString, string jsonString)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Accept", "application/json");
                client.DefaultRequestHeaders.Add("Log-Type", _logName);
                client.DefaultRequestHeaders.Add("Authorization", signature);
                client.DefaultRequestHeaders.Add("x-ms-date", dateString);
                client.DefaultRequestHeaders.Add("time-generated-field", "");

                var httpContent = new StringContent(jsonString, Encoding.UTF8);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var response = await client.PostAsync(_analyticsUrl, httpContent);
                
                SelfLog.WriteLine(response.ReasonPhrase);
            }
        }
        /// <summary>
        /// Emit a batch of log events, running asynchronously.
        /// </summary>
        /// <param name="events">The events to send to the syslog service</param>
        public async Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            foreach (var logEvent in events)
            {
                var message = this.formatter.FormatMessage(logEvent);
                var data    = Encoding.UTF8.GetBytes(message);

                try
                {
                    await this.client.SendAsync(data, data.Length, this.endpoint).ConfigureAwait(false);
                }
                catch (SocketException ex)
                {
                    SelfLog.WriteLine($"[{nameof(SyslogTcpSink)}] error while sending log event to syslog {this.endpoint.Address}:{this.endpoint.Port} - {ex.Message}\n{ex.StackTrace}");
                }
            }
        }
示例#9
0
        private async Task Send(IEnumerable <LogEvent> events)
        {
            string allEvents = string.Empty;

            foreach (var logEvent in events)
            {
                var sw = new StringWriter();
                _jsonFormatter.Format(logEvent, sw);

                var serialisedEvent = sw.ToString();

                var splunkEvent = new SplunkEvent(serialisedEvent, _source, _sourceType, _host, _index, logEvent.Timestamp.ToEpoch());

                allEvents = $"{allEvents}{splunkEvent.Payload}";
            }
            var request = new EventCollectorRequest(_splunkHost, allEvents);

            var response = await _httpClient.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                //Do Nothing?
            }
            else
            {
                //Application Errors sent via HTTP Event Collector
                if (HttpEventCollectorApplicationErrors.Any(x => x == response.StatusCode))
                {
                    SelfLog.WriteLine(
                        "A status code of {0} was received when attempting to send to {1}.  The event has been discarded and will not be placed back in the queue.",
                        response.StatusCode.ToString(), _splunkHost);
                }
                else
                {
                    //Put the item back in the queue & retry on next go
                    SelfLog.WriteLine(
                        "A status code of {0} was received when attempting to send to {1}.  The event has been placed back in the queue",
                        response.StatusCode.ToString(), _splunkHost);

                    foreach (var logEvent in events)
                    {
                        _queue.Enqueue(logEvent);
                    }
                }
            }
        }
示例#10
0
        public string GetConnectionString(string nameOrConnectionString, IConfiguration appConfiguration)
        {
            // If there is an `=`, we assume this is a raw connection string not a named value
            // If there are no `=`, attempt to pull the named value from config
            if (nameOrConnectionString.IndexOf("=", StringComparison.InvariantCultureIgnoreCase) > -1)
            {
                return(nameOrConnectionString);
            }
            var cs = appConfiguration?.GetConnectionString(nameOrConnectionString);

            if (string.IsNullOrEmpty(cs))
            {
                SelfLog.WriteLine("MSSqlServer sink configured value {0} is not found in ConnectionStrings settings and does not appear to be a raw connection string.",
                                  nameOrConnectionString);
            }
            return(cs);
        }
        private bool WeAreAtEndOfTheFileAndItIsNotLockedByAnotherThread(string file, long nextLineBeginsAtOffset)
        {
            try
            {
                return(_fileManager.GetFileLengthExclusiveAccess(file) <= nextLineBeginsAtOffset);
            }
            catch (IOException ex)
            {
                SelfLog.WriteLine("Swallowed I/O exception while testing locked status of {0}", ex, file);
            }
            catch (Exception ex)
            {
                SelfLog.WriteLine("Unexpected exception while testing locked status of {0}", ex, file);
            }

            return(false);
        }
示例#12
0
        public static object Simplify(LogEventPropertyValue value)
        {
            if (value is ScalarValue scalar)
            {
                return(SimplifyScalar(scalar.Value));
            }

            if (value is DictionaryValue dict)
            {
                var result = new Dictionary <object, object>();
                foreach (var element in dict.Elements)
                {
                    var key = SimplifyScalar(element.Key.Value);
                    if (result.ContainsKey(key))
                    {
                        SelfLog.WriteLine("The key {0} is not unique in the provided dictionary after simplification to {1}.", element.Key, key);
                        return(dict.Elements.Select(e => new Dictionary <string, object>
                        {
                            { "Key", SimplifyScalar(e.Key.Value) },
                            { "Value", Simplify(e.Value) }
                        })
                               .ToArray());
                    }
                    result.Add(key, Simplify(element.Value));
                }
                return(result);
            }

            if (value is SequenceValue seq)
            {
                return(seq.Elements.Select(Simplify).ToArray());
            }

            if (!(value is StructureValue str))
            {
                return(null);
            }

            var props = str.Properties.ToDictionary(p => p.Name, p => Simplify(p.Value));

            if (str.TypeTag != null)
            {
                props["$typeTag"] = str.TypeTag;
            }
            return(props);
        }
示例#13
0
        static Dictionary <string, LogLevel> GetLevelOverrides(IConfiguration configuration)
        {
            var dictionary = new Dictionary <string, LogLevel>();

            foreach (IConfigurationSection configurationSection in configuration.GetSection("LogLevel").GetChildren().Where(cfg => cfg.Key != "Default"))
            {
                if (!Enum.TryParse <LogLevel>(configurationSection.Value, out var result))
                {
                    SelfLog.WriteLine("The level override setting `{0}` for `{1}` is invalid", configurationSection.Value, configurationSection.Key);
                }
                else
                {
                    dictionary[configurationSection.Key] = result;
                }
            }
            return(dictionary);
        }
示例#14
0
        public static void Main(string[] args)
        {
            SelfLog.Enable(Console.Error);

            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("appsettings.json", true, true)
                                .AddUserSecrets(typeof(Program).Assembly)
                                .AddEnvironmentVariables()
                                .Build();

            Log.Logger = new LoggerConfiguration()
                         .ReadFrom.Configuration(configuration)
                         .CreateLogger();

            CreateHostBuilder(args).Build().Run();
        }
示例#15
0
        protected override async Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            if (events == null)
            {
                return;
            }

            int count = 0;

            try
            {
                var logs = new List <dynamic>();
                foreach (var ev in events)
                {
                    count++;
                    var identity = ev.Properties["Identity"].ToString();
                    identity = identity.Substring(1, identity.Length - 2);
                    var nodeId = ev.Properties["NodeId"].ToString();
                    nodeId = nodeId.Substring(1, nodeId.Length - 2);

                    var logInfo = new
                    {
                        Exception = ev.Exception?.ToString(),
                        Identity  = identity,
                        Level     = ev.Level.ToString(),
                        Logged    = ev.Timestamp,
                        Message   = ev.RenderMessage(),
                        NodeId    = nodeId,
                        DateTime  = DateTime.Now
                    };
                    logs.Add(logInfo);
                }

                HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, _api);
                httpRequestMessage.Headers.Add("DotnetSpiderToken", _token);
                var json = JsonConvert.SerializeObject(logs);
                httpRequestMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");

                await HttpClientDownloader.Default.SendAsync(httpRequestMessage);
            }
            catch (Exception ex)
            {
                SelfLog.WriteLine("Unable to write {0} log events to the database due to following error: {1}", count,
                                  ex.Message);
            }
        }
        protected void SendMessage(string token, string chatId, TelegramMessage message)
        {
            SelfLog.WriteLine($"Trying to send message to chatId '{chatId}': '{message}'.");

            var telegramClient = TelegramClientFactory.CreateClient(token, proxy);

            var sendMessageTask = telegramClient.PostAsync(message: message, chatId: chatId);

            Task.WaitAll(sendMessageTask);

            var sendMessageResult = sendMessageTask.Result;

            if (sendMessageResult != null)
            {
                SelfLog.WriteLine($"Message sent to chatId '{chatId}': '{sendMessageResult.StatusCode}'.");
            }
        }
        private static Dictionary <string, LogLevel> GetLevelOverrides(IConfiguration configuration)
        {
            var levelOverrides = new Dictionary <string, LogLevel>();

            foreach (var overr in configuration.GetSection("LogLevel").GetChildren().Where(cfg => cfg.Key != "Default"))
            {
                if (!Enum.TryParse(overr.Value, out LogLevel value))
                {
                    SelfLog.WriteLine("The level override setting `{0}` for `{1}` is invalid", overr.Value, overr.Key);
                    continue;
                }

                levelOverrides[overr.Key] = value;
            }

            return(levelOverrides);
        }
        public override void Flush()
        {
            EnsureNotDisposed();

            base.Flush();

            var currentNotepadProcess = _currentNotepadProcess;
            var targetNotepadProcess  = _notepadProcessFinderFunc();

            if (currentNotepadProcess is null || targetNotepadProcess is null || currentNotepadProcess.Id != targetNotepadProcess.Id)
            {
                _currentNotepadProcess      = currentNotepadProcess = targetNotepadProcess;
                _currentNotepadEditorHandle = IntPtr.Zero;

                if (currentNotepadProcess is null || currentNotepadProcess.HasExited)
                {
                    // No instances of Notepad found... Nothing to do
                    return;
                }

                var notepadWindowHandle = currentNotepadProcess.MainWindowHandle;

                var notepadEditorHandle = User32.FindWindowEx(notepadWindowHandle, IntPtr.Zero, "Edit", null);
                if (notepadEditorHandle == IntPtr.Zero)
                {
                    SelfLog.WriteLine($"Unable to access a Notepad Editor on process {currentNotepadProcess.ProcessName} ({currentNotepadProcess.Id})");
                    return;
                }

                _currentNotepadEditorHandle = notepadEditorHandle;
            }

            // Get how many characters are in the Notepad editor already
            var textLength = User32.SendMessage(_currentNotepadEditorHandle, User32.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);

            // Set the caret position to the end of the text
            User32.SendMessage(_currentNotepadEditorHandle, User32.EM_SETSEL, (IntPtr)textLength, (IntPtr)textLength);

            var buffer  = base.GetStringBuilder();
            var message = buffer.ToString();

            // Write the log message to Notepad
            User32.SendMessage(_currentNotepadEditorHandle, User32.EM_REPLACESEL, (IntPtr)1, message);

            buffer.Clear();
        }
示例#19
0
        public void WhenWaitingShouldCancel_OnDispose()
        {
            var wasCalled        = false;
            var writtenToSelfLog = false;

            SelfLog.Enable(_ => writtenToSelfLog = true);

            using (var timer = new PortableTimer(async delegate { await Task.Delay(50); wasCalled = true; }))
            {
                timer.Start(TimeSpan.FromMilliseconds(20));
            }

            Thread.Sleep(100);

            Assert.False(wasCalled, "tick handler was called");
            Assert.False(writtenToSelfLog, "message was written to SelfLog");
        }
示例#20
0
        public LoggingModule(
            IConfiguration configuration,
            IServiceCollection services)
        {
            SelfLog.Enable(Console.WriteLine);

            Log.Logger = new LoggerConfiguration()
                         .ReadFrom.Configuration(configuration)
                         .Enrich.FromLogContext()
                         .Enrich.WithMachineName()
                         .Enrich.WithThreadId()
                         .Enrich.WithEnvironmentUserName()
                         .Destructure.JsonNetTypes()
                         .CreateLogger();

            services.AddLogging(l => l.AddSerilog(Log.Logger));
        }
        static async Task Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Information()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                         .Enrich.FromLogContext()
                         .WriteTo.InfluxDB("Test App"                // Application Name
                                           , "Test Instance"         // Instance or Environment Name
                                           , "http://localhost:8086" // InfluxDb Address
                                           , "_internal")            // InfluxDb Database Name)
                         .CreateLogger();

            await BuildCommandLine()
            .UseHost(_ => Host.CreateDefaultBuilder(),
                     host =>
            {
                SelfLog.Enable(System.Console.Out);
                host.UseSerilog((hostingContext, loggerConfiguration) =>
                {
                    // could be configured here, but prefer earlier configuration
                })
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    // The next 2 lines commented out as they are added by default in the correct order
                    // for more control first call config.Sources.Clear();
                    //config.AddJsonFile("appsettings.json", optional: true);
                    //config.AddEnvironmentVariables();
                    config.AddUserSecrets <Program>();
                    var configuration = config.Build();

                    if (args is null)
                    {
                        //add some defaults from config
                        var number = configuration.GetSection("Sample").GetValue <int>("number");
                        args       = new string[0];
                        args       = args.Append($"-n {number}").ToArray();
                    }

                    config.AddCommandLine(args);
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddOptions <SampleOptions>()
                    .Bind(hostContext.Configuration.GetSection(SampleOptions.Sample));
                });
            })
        bool TryAcquireMutex()
        {
            try
            {
                if (!_mutex.WaitOne(MutexWaitTimeout))
                {
                    SelfLog.WriteLine("Shared file mutex could not be acquired within {0} ms", MutexWaitTimeout);
                    return(false);
                }
            }
            catch (AbandonedMutexException)
            {
                SelfLog.WriteLine("Inherited shared file mutex after abandonment by another process");
            }

            return(true);
        }
        public void CarriesMessageTemplateProperties()
        {
            var selfLog = new StringWriter();

            SelfLog.Enable(selfLog);

            var(logger, sink) = SetUp(LogLevel.Trace);

            logger.LogInformation("Hello, {Recipient}", "World");

            Assert.True(sink.Writes[0].Properties.ContainsKey("Recipient"));
            Assert.Equal("\"World\"", sink.Writes[0].Properties["Recipient"].ToString());
            Assert.Equal("Hello, {Recipient}", sink.Writes[0].MessageTemplate.Text);

            SelfLog.Disable();
            Assert.Empty(selfLog.ToString());
        }
        protected void SendMessage(string token, string chatId, TelegramMessage message)
        {
            SelfLog.WriteLine($"Trying to send message to chatId '{chatId}': '{message}'.");

            var client = new TelegramBot(botToken: token, timeoutSeconds: 5);

            var sendMessageTask = client.PostAsync(message: message, chatId: chatId);

            Task.WaitAll(sendMessageTask);

            var sendMessageResult = sendMessageTask.Result;

            if (sendMessageResult != null)
            {
                SelfLog.WriteLine($"Message sent to chatId '{chatId}': '{sendMessageResult.StatusCode}'.");
            }
        }
示例#25
0
        IEnumerable <LogEventProperty> ConstructPositionalProperties(MessageTemplate template, object[] messageTemplateParameters)
        {
            var positionalProperties = template.PositionalProperties;

            if (positionalProperties.Length != messageTemplateParameters.Length)
            {
                SelfLog.WriteLine("Positional property count does not match parameter count: {0}", template);
            }

            var result = new LogEventProperty[messageTemplateParameters.Length];

            foreach (var property in positionalProperties)
            {
                int position;
                if (property.TryGetPositionalValue(out position))
                {
                    if (position < 0 || position >= messageTemplateParameters.Length)
                    {
                        SelfLog.WriteLine("Unassigned positional value {0} in: {1}", position, template);
                    }
                    else
                    {
                        result[position] = ConstructProperty(property, messageTemplateParameters[position]);
                    }
                }
            }

            var next = 0;

            for (var i = 0; i < result.Length; ++i)
            {
                if (result[i] != null)
                {
                    result[next] = result[i];
                    ++next;
                }
            }

            if (next != result.Length)
            {
                Array.Resize(ref result, next);
            }

            return(result);
        }
        async void OnTick()
        {
            try
            {
                lock (_stateLock)
                {
                    if (_disposed)
                    {
                        return;
                    }

                    // There's a little bit of raciness here, but it's needed to support the
                    // current API, which allows the tick handler to reenter and set the next interval.

                    if (_running)
                    {
                        Monitor.Wait(_stateLock);

                        if (_disposed)
                        {
                            return;
                        }
                    }

                    _running = true;
                }

                if (!_cancel.Token.IsCancellationRequested)
                {
                    await _onTick(_cancel.Token);
                }
            }
            catch (OperationCanceledException tcx)
            {
                SelfLog.WriteLine("The timer was canceled during invocation: {0}", tcx);
            }
            finally
            {
                lock (_stateLock)
                {
                    _running = false;
                    Monitor.PulseAll(_stateLock);
                }
            }
        }
示例#27
0
        public LogEventPropertyValue CreatePropertyValue(object value, Destructuring destructuring)
        {
            try
            {
                return(CreatePropertyValue(value, destructuring, 1));
            }
            catch (Exception ex)
            {
                SelfLog.WriteLine("Exception caught while converting property value: {0}", ex);

                if (_propagateExceptions)
                {
                    throw;
                }

                return(new ScalarValue("Capturing the property value threw an exception: " + ex.GetType().Name));
            }
        }
示例#28
0
        /// <summary>
        /// Emit a batch of log events.
        /// </summary>
        /// <param name="events">The events to emit.</param>
        /// <remarks>Override either <see cref="PeriodicBatchingSink.EmitBatch"/> or <see cref="PeriodicBatchingSink.EmitBatchAsync"/>,
        /// not both.</remarks>
        protected override void EmitBatch(IEnumerable <Events.LogEvent> events)
        {
            // This sink doesn't actually write batches, instead only using
            // the PeriodicBatching infrastructure to manage background work.
            // Probably needs modification.

            foreach (var logEvent in events)
            {
                var key = Guid.NewGuid().ToString();

                var result = _couchbaseClient.StoreJson(Enyim.Caching.Memcached.StoreMode.Add, key, new LogEvent(logEvent, logEvent.RenderMessage(_formatProvider)));

                if (!result)
                {
                    SelfLog.WriteLine("Failed to store value");
                }
            }
        }
示例#29
0
        private static IDictionary <string, string> LogEventPropertiesToNewRelicExceptionProperties(LogEvent logEvent)
        {
            var properties = new Dictionary <string, string>();

            foreach (var source in logEvent.Properties.Where(p => p.Value != null))
            {
                var safeKey = source.Key.ToNewRelicSafeString();
                if (properties.ContainsKey(safeKey))
                {
                    SelfLog.WriteLine("Transformed property {0} from event with message template {1} is already in dictionary", safeKey, logEvent.MessageTemplate.Text);
                    continue;
                }

                properties.Add(safeKey, source.Value.ToString());
            }

            return(properties);
        }
示例#30
0
        protected void SendMessageWithWebHooks(string webhookUri, string message)
        {
            SelfLog.WriteLine("Trying to send message to webhook '{0}': '{1}'.", webhookUri, message);

            if (!string.IsNullOrWhiteSpace(message))
            {
                var slackClient = new SlackClient(webhookUri, 25);

                var sendMessageTask = slackClient.PostAsync(message);
                Task.WaitAll(sendMessageTask);

                var sendMessageResult = sendMessageTask.Result;
                if (sendMessageResult != null)
                {
                    SelfLog.WriteLine("Message sent to webhook '{0}': '{1}'.", webhookUri, sendMessageResult.StatusCode);
                }
            }
        }