/// <summary> /// Creates a new logger and logger provider from a newly instantiated LoggerFactory. /// </summary> /// <param name="serviceName">name of the service. Is passed to the logger provider.</param> /// <returns>Tuple of logger and corresponding loggerProvider</returns> public static (ILogger logger, InMemoryLoggerProvider loggerProvider) CreateLogger(string serviceName, string logTopic, string logTopicKey) { if (logTopic != null) { string topicEndpoint = logTopic; string topicKey = logTopicKey; _topicHostname = new Uri(topicEndpoint).Host; TopicCredentials topicCredentials = new TopicCredentials(topicKey); _eventGridClient = new EventGridClient(topicCredentials); } var factory = new LoggerFactory(); var loggerProvider = new InMemoryLoggerProvider(); factory.AddProvider(loggerProvider); return(factory.CreateLogger(serviceName), loggerProvider); }
public static async Task <string> SendLogEvent(string eventId, InMemoryLoggerProvider logger, string subjectPostfix, string eventType = LogEventName, bool silent = true) { using (MiniProfiler.Current.Step(nameof(SendLogEvent))) { try { string subject = DEFAULT_SUBJECT; if (!string.IsNullOrEmpty(subjectPostfix)) { subject += subjectPostfix; } IList <EventGridEvent> eventList = new List <EventGridEvent>(); foreach (var msg in logger.Messages) { dynamic eventData = new ExpandoObject(); eventData.eventid = eventId; eventData.message = msg; var newId = Guid.NewGuid().ToString(); eventList.Add(new EventGridEvent() { Id = newId, EventType = eventType, Data = eventData, EventTime = DateTime.UtcNow, Subject = subject, DataVersion = "2.0" }); } await _eventGridClient.PublishEventsAsync(_topicHostname, eventList); return("OK"); } catch (Exception e) { if (silent) { return(e.Message); } else { throw e; } } } }
/// <summary> /// POSTs messages of <paramref name="logger"/> to server specified in <paramref name="url"/>. /// </summary> /// <param name="url">Uri of the server</param> /// <param name="logger">logger containing messages</param> /// <param name="certificate">certificate used in HTTP POST header</param> /// <returns>HttpClient response of POST</returns> public static async Task <HttpResponseMessage> SendLogToServer(Uri url, InMemoryLoggerProvider logger, string certificate) { var cert = certificate; var request = new HttpRequestMessage() { Method = HttpMethod.Post, Content = new StringContent(JsonConvert.SerializeObject(logger.Messages)), RequestUri = url }; if (request.Headers.Contains(HeaderNames.Auth.XArrClientCert)) { request.Headers.Remove(HeaderNames.Auth.XArrClientCert); } request.Headers.Add(HeaderNames.Auth.XArrClientCert, cert); return(await httpClient.SendAsync(request)); }
/// <summary> /// POSTs messages of <paramref name="logger"/> to server specified in <paramref name="url"/>. /// </summary> /// <param name="url">Uri of the server</param> /// <param name="logger">logger containing messages</param> /// <param name="certificate">certificate used in HTTP POST header</param> /// <returns>HttpClient response of POST</returns> public static async Task <HttpResponseMessage> SendLogToServerWithToken(Uri url, InMemoryLoggerProvider logger, string token, string apiKey) { var request = new HttpRequestMessage() { Method = HttpMethod.Post, RequestUri = url, Content = new StringContent(JsonConvert.SerializeObject(logger.Messages)) }; if (request.Headers.Contains(HeaderNames.Auth.Authorization)) { request.Headers.Remove(HeaderNames.Auth.Authorization); } request.Headers.Add(HeaderNames.Auth.Authorization, "Bearer " + token); if (request.Headers.Contains(HeaderNames.Auth.HfAuthorization)) { request.Headers.Remove(HeaderNames.Auth.HfAuthorization); } request.Headers.Add(HeaderNames.Auth.HfAuthorization, "Bearer " + token); if (request.Headers.Contains(HeaderNames.Azure.SUBSCRIPTION_KEY) && apiKey != null) { request.Headers.Remove(HeaderNames.Azure.SUBSCRIPTION_KEY); } request.Headers.Add(HeaderNames.Azure.SUBSCRIPTION_KEY, apiKey); return(await httpClient.SendAsync(request)); }
public static async Task <HttpResponseMessage> SendLogToServer(string URL, InMemoryLoggerProvider logger, string certificate) { Uri uri = new Uri(URL); return(await SendLogToServer(uri, logger, certificate)); }
public InMemoryLogger(InMemoryLoggerProvider provider, string categoryName) { _provider = provider; _categoryName = categoryName; }