private void AddEntry(LogEntry entry, object context) { // Check whether the message contains null characters. If it does, crop it, because it's probably broken. int nullCharIndex = entry.Message.IndexOf('\0'); if (nullCharIndex != -1) { entry.Message = entry.Message .Substring(0, Math.Min(nullCharIndex, 50)) + " | Contains '\0' and is likely broken."; } // Forward the message to all outputs lock (_outputLock) { for (int i = 0; i < _outputs.Count; i++) { ILogOutput log = _outputs[i]; try { log.Write(this, entry, context); } catch (Exception) { // Don't allow log outputs to throw unhandled exceptions, // because they would result in another log - and more exceptions. } } } }
/// <summary> /// Accepts a connection and processes request. /// This is NOT thread-safe already. WIP /// </summary> /// <returns>A task that is completed when connection is succesfully accepted.</returns> public Task Accept() { output.Write( new Str("SingleThreaded.Accept, thread: "), new ThreadId() ); if (task != null && task.Task != null && task.Task.IsCompletedSuccessfully) { throw new ConcurrencyException("Concurrent calls to SingleThreaded.Accept are impossible"); } this.task = new TaskCompletionSource <bool>(false); SocketAsyncEventArgs socketOp = new SocketAsyncEventArgs(); try { socketOp.Completed += SocketOp_Completed; socketOp.UserToken = new AsyncUserToken(); socketOp.AcceptSocket = null; transport.AcceptAsync(socketOp); } catch (System.Exception exc) { output.Write(new ToStr(exc)).Wait(); socketOp.Completed -= SocketOp_Completed; task.TrySetException(exc); } return(task.Task ?? Task.CompletedTask); }