/// <summary> /// This is run once before the first time OnRun is run. /// If an exception is thrown here, OnRun will never start running, /// and this AppProcess will be removed from the running processes pool. /// </summary> protected virtual async Task OnInit(ActorUtil util) { await Task.FromResult(0); }
/// <summary> /// This is run at approximately RunDelay() intervals (probably slightly slower.). /// </summary> /// <returns></returns> protected abstract Task OnRun(ActorUtil util);
protected async override Task OnInit(ActorUtil util) { await Task.FromResult(0); }
protected async override Task OnRun(ActorUtil util) { List <ActinLog> toWrite = null; lock (lockEverything) { if (queuedLogs.Count == 0) { return; } toWrite = new List <ActinLog>(queuedLogs); queuedLogs.Clear(); } var fileInfo = getTodaysLogFile(util.Started); if (fileInfo == null) { return; } if (!fileInfo.Directory.Exists) { fileInfo.Directory.Create(); } using (var stream = File.OpenWrite(fileInfo.FullName)) { var endBytes = Encoding.UTF8.GetBytes("</Logs>"); if (stream.Length > 0) { stream.Seek(-endBytes.Length, SeekOrigin.End); } else { var startBytes = Encoding.UTF8.GetBytes("<Logs>" + Environment.NewLine); await stream.WriteAsync(startBytes, 0, startBytes.Length); } var sb = new StringBuilder(); var bytes = new byte[0]; async Task writeToDisk() { if (sb.Length == 0) { return; } var text = sb.ToString(); sb.Clear(); if (bytes.Length < (4 * text.Length)) { bytes = new byte[text.Length * 4]; } int len = Encoding.UTF8.GetBytes(text, 0, text.Length, bytes, 0); sb.Clear(); await stream.WriteAsync(bytes, 0, len); } string getEscaped(string x) { if (x.Contains("&")) { x = x.Replace("&", "&"); } if (x.Contains("<")) { x = x.Replace("<", "<"); } if (x.Contains(">")) { x = x.Replace(">", ">"); } if (x.Contains("\"")) { x = x.Replace("\"", """); } if (x.Contains("'")) { x = x.Replace("'", "'"); } return(x); } foreach (var log in toWrite) { sb.Append("<Log time=\""); sb.Append(getEscaped(log.Time.ToString())); sb.Append("\" type=\""); sb.Append(log.Type.ToString()); sb.Append("\" location=\""); sb.Append(getEscaped(log.Location)); sb.Append("\" context=\""); sb.Append(getEscaped(log.Context)); sb.AppendLine("\">"); if (!string.IsNullOrEmpty(log.UserMessage)) { sb.Append(" "); sb.AppendLine(getEscaped(log.UserMessage)); } if (!string.IsNullOrEmpty(log.Details)) { sb.Append(" "); sb.AppendLine(getEscaped(log.Details)); } sb.AppendLine("</Log>"); if (sb.Length > 10000) { await writeToDisk(); } } await writeToDisk(); await stream.WriteAsync(endBytes, 0, endBytes.Length); } }
protected async override Task OnDispose(ActorUtil util) { //Make sure any final logs get written. await this.OnRun(util); }