public void Send(DateTime now) { var userSettings = Properties.Settings.Default.UserId; if (userSettings == null || userSettings.Trim() == string.Empty) { Logger.Error("UserId is null!!!!"); return; } var userId = new Guid(userSettings); var usage = new UserUsage { Start = _startTime, AppNames = _appNames.Keys.ToArray(), AppUsedNameHashCodes = _appsUsed.ToArray(), AppUsedSeconds = _appUsedTimes.ToArray(), UserId = userId }; _transporter.SendAsync(usage); Logger.DebugFormat( "Send - [_appNames:{0}] [_appsUsed : {1}] [_appUsedTimes: {2}] [_appUsedTimes.Sum(): {3}]", _appNames.Count, _appsUsed.Count, _appUsedTimes.Count, _appUsedTimes.Sum()); Logger.LogUsage(usage); Start(now); }
public static void LogUsage(UserUsage logUsage) { var mappings = new Dictionary <uint, string>(); foreach (string app in logUsage.AppNames) { mappings[StringUtils.HashString(app)] = app; } using (FileStream fileStream = File.Open("compare-to-html.csv", FileMode.Append)) using (var writer = new StreamWriter(fileStream)) { writer.Write(logUsage.Start.Ticks.ToString(CultureInfo.InvariantCulture)); writer.Write(","); writer.Write(logUsage.Start.ToString(CultureInfo.InvariantCulture).Replace('"', '-')); writer.Write(','); writer.Write("AppName,Seconds"); writer.WriteLine(); for (int i = 0; i < logUsage.AppUsedNameHashCodes.Length; i++) { var hashCode = logUsage.AppUsedNameHashCodes[i]; var seconds = logUsage.AppUsedSeconds[i]; writer.Write(",,,"); writer.Write(mappings[hashCode]); writer.Write(','); writer.Write(seconds); writer.WriteLine(); } } }
public void SendAsync(UserUsage usage) { lock (lockObj) { queue.Enqueue(usage); } }
public static void LogUsage(UserUsage logUsage) { var mappings = new Dictionary<uint, string>(); foreach (string app in logUsage.AppNames) { mappings[StringUtils.HashString(app)] = app; } using (FileStream fileStream = File.Open("compare-to-html.csv", FileMode.Append)) using (var writer = new StreamWriter(fileStream)) { writer.Write(logUsage.Start.Ticks.ToString(CultureInfo.InvariantCulture)); writer.Write(","); writer.Write(logUsage.Start.ToString(CultureInfo.InvariantCulture).Replace('"', '-')); writer.Write(','); writer.Write("AppName,Seconds"); writer.WriteLine(); for (int i = 0; i < logUsage.AppUsedNameHashCodes.Length; i++) { var hashCode = logUsage.AppUsedNameHashCodes[i]; var seconds = logUsage.AppUsedSeconds[i]; writer.Write(",,,"); writer.Write(mappings[hashCode]); writer.Write(','); writer.Write(seconds); writer.WriteLine(); } } }
private static bool TrySend(UserUsage usage) { try { var httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(new Uri(Config.Urls), "api/data")); httpWebRequest.ContentType = "text/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = GetJson(usage.Start, usage.UserId, usage.AppNames, usage.AppUsedNameHashCodes, usage.AppUsedSeconds); streamWriter.Write(json); streamWriter.Flush(); streamWriter.Close(); var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); if (httpResponse.StatusCode != HttpStatusCode.OK && httpResponse.StatusCode != HttpStatusCode.NoContent) { Logger.ErrorFormat("Response code was no 200 or 204: {0}", httpResponse.StatusCode); } } } catch (Exception ex) { Logger.Error("Exception occured during SendAsync", ex); return(false); } return(true); }
private void Retry() { bool success = TrySend(retry); if (success) { retry = null; numRetries = 0; } else { numRetries++; if (numRetries > 5) { retry = null; numRetries = 0; Logger.Error("Failed to send after 5 times!"); } } }
private void Send() { UserUsage firstOne = null; lock (lockObj) { if (queue.Count > 0) { firstOne = queue.Dequeue(); } } if (firstOne != null) { bool success = TrySend(firstOne); if (!success) { retry = firstOne; } } }
private static bool TrySend(UserUsage usage) { // Log to Web Service/Azure/etc return true; }
public static void LogUsage(UserUsage logUsage) { }
private static bool TrySend(UserUsage usage) { // Log to Web Service/Azure/etc return(true); }