I am trying to trap the request/response xml of my soap communication. I would love to have used WCF, but must comply with the employer request.
So, I have this sample asmx website with this webmethod in my service:
[WebMethod]
public int Multiply(int x, int y)
{
return x * y;
}
The site is added to IIS and I can see it in browser:
http://localhost/WebService/Service.asmx?op=Multiply
and it works when I give it two int values.
I am trying to trap the SOAP body request that is getting sent and received. According to my research, if I add a soap extension, I would be able to do that. So, I am looking at this example: https://stackoverflow.com/a/10613433/1019042 and trying to imitate it.
I added a class library project to my solution with this class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using System.IO;
using System.Reflection;
namespace SoapExtensions
{
public class SoapLoggerExtension : SoapExtension
{
//private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private Stream oldStream;
private Stream newStream;
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
{
WriteMessages("inside the getinitializer1");
return null;
}
public override object GetInitializer(Type serviceType)
{
WriteMessages("inside the getinitializer2");
WriteMessages("inside the getinitializer");
return null;
}
public override void Initialize(object initializer)
{
WriteMessages("inside the Initialize");
}
public override System.IO.Stream ChainStream(System.IO.Stream stream)
{
WriteMessages("inside the ChainStream");
oldStream = stream;
newStream = new MemoryStream();
return newStream;
}
public override void ProcessMessage(SoapMessage message)
{
WriteMessages("inside the ProcessMessage");
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
Log(message, "AfterSerialize");
CopyStream(newStream, oldStream);
newStream.Position = 0;
break;
case SoapMessageStage.BeforeDeserialize:
CopyStream(oldStream, newStream);
Log(message, "BeforeDeserialize");
break;
case SoapMessageStage.AfterDeserialize:
break;
}
}
private void WriteMessages(string message)
{
StreamWriter sw = new StreamWriter(#"C:\MyFolder\LogFolder\myLog.txt", true);
sw.WriteLine(message);
sw.Close();
}
public void Log(SoapMessage message, string stage)
{
newStream.Position = 0;
string contents = (message is SoapServerMessage) ? "SoapRequest " : "SoapResponse ";
contents += stage + ";";
StreamReader reader = new StreamReader(newStream);
contents += reader.ReadToEnd();
newStream.Position = 0;
WriteMessages(contents.ToString());
//log.Debug(contents);
}
void ReturnStream()
{
WriteMessages("inside the ReturnStream");
CopyAndReverse(newStream, oldStream);
}
void ReceiveStream()
{
WriteMessages("inside the ReceiveStream");
CopyAndReverse(newStream, oldStream);
}
public void ReverseIncomingStream()
{
WriteMessages("inside the ReverseIncomingStream");
ReverseStream(newStream);
}
public void ReverseOutgoingStream()
{
WriteMessages("inside the ReverseOutgoingStream");
ReverseStream(newStream);
}
public void ReverseStream(Stream stream)
{
WriteMessages("inside the ReverseStream");
TextReader tr = new StreamReader(stream);
string str = tr.ReadToEnd();
char[] data = str.ToCharArray();
Array.Reverse(data);
string strReversed = new string(data);
TextWriter tw = new StreamWriter(stream);
stream.Position = 0;
tw.Write(strReversed);
tw.Flush();
}
void CopyAndReverse(Stream from, Stream to)
{
WriteMessages("inside the CopyAndReverse");
TextReader tr = new StreamReader(from);
TextWriter tw = new StreamWriter(to);
string str = tr.ReadToEnd();
char[] data = str.ToCharArray();
Array.Reverse(data);
string strReversed = new string(data);
tw.Write(strReversed);
tw.Flush();
}
private void CopyStream(Stream fromStream, Stream toStream)
{
WriteMessages("inside the CopyStream");
try
{
StreamReader sr = new StreamReader(fromStream);
StreamWriter sw = new StreamWriter(toStream);
sw.WriteLine(sr.ReadToEnd());
sw.Flush();
}
catch (Exception ex)
{
string message = String.Format("CopyStream failed because: {0}", ex.Message);
WriteMessages("Message is: " + message);
WriteMessages("ex is: " + ex);
// log.Error(message, ex);
}
}
}
[AttributeUsage(AttributeTargets.Method)]
public class SoapLoggerExtensionAttribute : SoapExtensionAttribute
{
private int priority = 1;
public override int Priority
{
get { return priority; }
set { priority = value; }
}
public override System.Type ExtensionType
{
get { return typeof(SoapLoggerExtension); }
}
}
}
I added a message to every method just to see if anything gets called in my extension.
and then went to my service and added a reference to the project. I went to the web.config of the service and added this under system.web:
<webServices>
<soapExtensionTypes>
<add type="SoapExtensions.SoapLoggerExtension, SoapExtension"
priority="1" group="0" />
</soapExtensionTypes>
</webServices>
The problem is that I don't get anything written in the mylog.txt like it is not hitting my soapservice.
The file resides in a folder that Everyone has full access to it.
Shouldn't the call to the Multiply log something via the soapextension? Are my settings wrong? Is there something else I should be doing?
I am using framework 3.5
try with
<webServices>
<soapExtensionTypes>
<add type="SoapExtensions.SoapLoggerExtension, SoapExtensions"
priority="1" group="0" />
</soapExtensionTypes>
</webServices>
you're missing the 's' in the assembly name for the type.
Also, once you start up the browser at http://localhost/WebService/Service.asmx?op=Multiply, use Visual Studio and attach to the process.
This way, you can debug the incoming multiple call and trace through the flow. you'll see if the Extension point was hit.
Related
I have a program which outputs various results onto a command line console.
How do I save the output to a text file using a StreamReader or other techniques?
System.Collections.Generic.IEnumerable<String> lines = File.ReadAllLines(#"C:\Test\ntfs8.txt");
foreach (String r in lines.Skip(1))
{
String[] token = r.Split(',');
String[] datetime = token[0].Split(' ');
String timeText = datetime[4];
String actions = token[2];
Console.WriteLine("The time for this array is: " + timeText);
Console.WriteLine(token[7]);
Console.WriteLine(actions);
MacActions(actions);
x = 1;
Console.WriteLine("================================================");
}
if (x == 2)
{
Console.WriteLine("The selected time does not exist within the log files!");
}
System.IO.StreamReader reader = ;
string sRes = reader.ReadToEnd();
StreamWriter SW;
SW = File.CreateText("C:\\temp\\test.bodyfile");
SW.WriteLine(sRes);
SW.Close();
Console.WriteLine("File Created");
reader.Close();
Try this example from this article - Demonstrates redirecting the Console output to a file
using System;
using System.IO;
static public void Main ()
{
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;
try
{
ostrm = new FileStream ("./Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter (ostrm);
}
catch (Exception e)
{
Console.WriteLine ("Cannot open Redirect.txt for writing");
Console.WriteLine (e.Message);
return;
}
Console.SetOut (writer);
Console.WriteLine ("This is a line of text");
Console.WriteLine ("Everything written to Console.Write() or");
Console.WriteLine ("Console.WriteLine() will be written to a file");
Console.SetOut (oldOut);
writer.Close();
ostrm.Close();
Console.WriteLine ("Done");
}
Try if this works:
FileStream filestream = new FileStream("out.txt", FileMode.Create);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);
For the question:
How to save Console.Writeline Outputs
to text file?
I would use Console.SetOut as others have mentioned.
However, it looks more like you are keeping track of your program flow. I would consider using Debug or Trace for keeping track of the program state.
It works similar the console except you have more control over your input such as WriteLineIf.
Debug will only operate when in debug mode where as Trace will operate in both debug or release mode.
They both allow for listeners such as output files or the console.
TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(tr1);
TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText("Output.txt"));
Debug.Listeners.Add(tr2);
-http://support.microsoft.com/kb/815788
do you want to write code for that or just use command-line feature 'command redirection' as follows:
app.exe >> output.txt
as demonstrated here: http://discomoose.org/2006/05/01/output-redirection-to-a-file-from-the-windows-command-line/ (Archived at archive.org)
EDIT: link dead, here's another example: http://pcsupport.about.com/od/commandlinereference/a/redirect-command-output-to-file.htm
Based in the answer by WhoIsNinja:
This code will output both into the Console and into a Log string that can be saved into a file, either by appending lines to it or by overwriting it.
The default name for the log file is 'Log.txt' and is saved under the Application path.
public static class Logger
{
public static StringBuilder LogString = new StringBuilder();
public static void WriteLine(string str)
{
Console.WriteLine(str);
LogString.Append(str).Append(Environment.NewLine);
}
public static void Write(string str)
{
Console.Write(str);
LogString.Append(str);
}
public static void SaveLog(bool Append = false, string Path = "./Log.txt")
{
if (LogString != null && LogString.Length > 0)
{
if (Append)
{
using (StreamWriter file = System.IO.File.AppendText(Path))
{
file.Write(LogString.ToString());
file.Close();
file.Dispose();
}
}
else
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(Path))
{
file.Write(LogString.ToString());
file.Close();
file.Dispose();
}
}
}
}
}
Then you can use it like this:
Logger.WriteLine("==========================================================");
Logger.Write("Loading 'AttendPunch'".PadRight(35, '.'));
Logger.WriteLine("OK.");
Logger.SaveLog(true); //<- default 'false', 'true' Append the log to an existing file.
Create a class Logger(code below), replace Console.WriteLine with Logger.Out.
At the end write to a file the string Log
public static class Logger
{
public static StringBuilder LogString = new StringBuilder();
public static void Out(string str)
{
Console.WriteLine(str);
LogString.Append(str).Append(Environment.NewLine);
}
}
Use Console.SetOut to redirect to a TextWriter as described here:
http://msdn.microsoft.com/en-us/library/system.console.setout.aspx
Necromancing.
I usually just create a class, which I can wrap around main in an IDisposable.
So I can log the console output to a file without modifying the rest of the code.
That way, I have the output in both the console and for later reference in a text-file.
public class Program
{
public static async System.Threading.Tasks.Task Main(string[] args)
{
using (ConsoleOutputMultiplexer co = new ConsoleOutputMultiplexer())
{
// Do something here
System.Console.WriteLine("Hello Logfile and Console 1 !");
System.Console.WriteLine("Hello Logfile and Console 2 !");
System.Console.WriteLine("Hello Logfile and Console 3 !");
} // End Using co
System.Console.WriteLine(" --- Press any key to continue --- ");
System.Console.ReadKey();
await System.Threading.Tasks.Task.CompletedTask;
} // End Task Main
}
with
public class MultiTextWriter
: System.IO.TextWriter
{
protected System.Text.Encoding m_encoding;
protected System.Collections.Generic.IEnumerable<System.IO.TextWriter> m_writers;
public override System.Text.Encoding Encoding => this.m_encoding;
public override System.IFormatProvider FormatProvider
{
get
{
return base.FormatProvider;
}
}
public MultiTextWriter(System.Collections.Generic.IEnumerable<System.IO.TextWriter> textWriters, System.Text.Encoding encoding)
{
this.m_writers = textWriters;
this.m_encoding = encoding;
}
public MultiTextWriter(System.Collections.Generic.IEnumerable<System.IO.TextWriter> textWriters)
: this(textWriters, textWriters.GetEnumerator().Current.Encoding)
{ }
public MultiTextWriter(System.Text.Encoding enc, params System.IO.TextWriter[] textWriters)
: this((System.Collections.Generic.IEnumerable<System.IO.TextWriter>)textWriters, enc)
{ }
public MultiTextWriter(params System.IO.TextWriter[] textWriters)
: this((System.Collections.Generic.IEnumerable<System.IO.TextWriter>)textWriters)
{ }
public override void Flush()
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
thisWriter.Flush();
}
}
public async override System.Threading.Tasks.Task FlushAsync()
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
await thisWriter.FlushAsync();
}
await System.Threading.Tasks.Task.CompletedTask;
}
public override void Write(char[] buffer, int index, int count)
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
thisWriter.Write(buffer, index, count);
}
}
public override void Write(System.ReadOnlySpan<char> buffer)
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
thisWriter.Write(buffer);
}
}
public async override System.Threading.Tasks.Task WriteAsync(char[] buffer, int index, int count)
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
await thisWriter.WriteAsync(buffer, index, count);
}
await System.Threading.Tasks.Task.CompletedTask;
}
public async override System.Threading.Tasks.Task WriteAsync(System.ReadOnlyMemory<char> buffer, System.Threading.CancellationToken cancellationToken = default)
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
await thisWriter.WriteAsync(buffer, cancellationToken);
}
await System.Threading.Tasks.Task.CompletedTask;
}
protected override void Dispose(bool disposing)
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
thisWriter.Dispose();
}
}
public async override System.Threading.Tasks.ValueTask DisposeAsync()
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
await thisWriter.DisposeAsync();
}
await System.Threading.Tasks.Task.CompletedTask;
}
public override void Close()
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
thisWriter.Close();
}
} // End Sub Close
} // End Class MultiTextWriter
public class ConsoleOutputMultiplexer
: System.IDisposable
{
protected System.IO.TextWriter m_oldOut;
protected System.IO.FileStream m_logStream;
protected System.IO.StreamWriter m_logWriter;
protected MultiTextWriter m_multiPlexer;
public ConsoleOutputMultiplexer()
{
this.m_oldOut = System.Console.Out;
try
{
this.m_logStream = new System.IO.FileStream("./Redirect.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
this.m_logWriter = new System.IO.StreamWriter(this.m_logStream);
this.m_multiPlexer = new MultiTextWriter(this.m_oldOut.Encoding, this.m_oldOut, this.m_logWriter);
System.Console.SetOut(this.m_multiPlexer);
}
catch (System.Exception e)
{
System.Console.WriteLine("Cannot open Redirect.txt for writing");
System.Console.WriteLine(e.Message);
return;
}
} // End Constructor
void System.IDisposable.Dispose()
{
System.Console.SetOut(this.m_oldOut);
if (this.m_multiPlexer != null)
{
this.m_multiPlexer.Flush();
if (this.m_logStream != null)
this.m_logStream.Flush();
this.m_multiPlexer.Close();
}
if(this.m_logStream != null)
this.m_logStream.Close();
} // End Sub Dispose
} // End Class ConsoleOutputMultiplexer
Using only configuration in your app.config:
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener"/>
<!--
<add name="logListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
<add name="EventLogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="MyEventLog"/>
-->
<!--
Remove the Default listener to avoid duplicate messages
being sent to the debugger for display
-->
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
For testing, you can use DebugView before running the program, then we can easily view all of the log messages.
References:
http://blogs.msdn.com/b/jjameson/archive/2009/06/18/configuring-logging-in-a-console-application.aspx
http://www.thejoyofcode.com/from_zero_to_logging_with_system_diagnostics_in_15_minutes.aspx
Redirect Trace output to Console
Problem redirecting debug output to a file using trace listener
https://ukadcdiagnostics.codeplex.com/
http://geekswithblogs.net/theunstablemind/archive/2009/09/09/adventures-in-system.diagnostics.aspx
I am currently trying to figure out, what´s the best way to log the user input as well as the output generated by the console.
My current approach is using :
class ConsoleCopy : IDisposable
{
FileStream fileStream;
StreamWriter fileWriter;
TextWriter doubleWriter;
TextWriter oldOut;
class DoubleWriter : TextWriter
{
TextWriter one;
TextWriter two;
public DoubleWriter(TextWriter one, TextWriter two)
{
this.one = one;
this.two = two;
}
public override Encoding Encoding
{
get { return one.Encoding; }
}
public override void Flush()
{
one.Flush();
two.Flush();
}
public override void Write(char value)
{
one.Write(value);
two.Write(value);
}
}
public ConsoleCopy(string path)
{
oldOut = Console.Out;
try
{
fileStream = File.Create(path);
fileWriter = new StreamWriter(fileStream);
fileWriter.AutoFlush = true;
doubleWriter = new DoubleWriter(fileWriter, oldOut);
}
catch (Exception e)
{
Console.WriteLine("Cannot open file for writing");
Console.WriteLine(e.Message);
return;
}
Console.SetOut(doubleWriter);
}
public void Dispose()
{
Console.SetOut(oldOut);
if (fileWriter != null)
{
fileWriter.Flush();
fileWriter.Close();
fileWriter = null;
}
if (fileStream != null)
{
fileStream.Close();
fileStream = null;
}
}
}
and
using (var cc = new ConsoleCopy(log))
{
//some code
Conole.WriteLine(string);
Console.WriteLine(string2);
string input = Console.ReadLine();
}
This approach is working fine for the console output, but I still can not figure out, how to add the user input.
I am redirecting the console output to the file while also being able to see it in the console. That is what I want to achieve with the uesr input aswell.
Thanks a lot!
The same way, actually.
Note that this class only implements ReadLine() for now:
class LoggingReader : TextReader
{
TextReader old;
TextWriter log;
public LoggingReader(TextReader old, TextWriter log)
{
this.old = old;
this.log = log;
}
public override string ReadLine()
{
string input = old.ReadLine();
log.Write("> {0}\r\n", input);
return input;
}
}
These are added to ConsoleCopy as members:
TextReader oldIn;
TextReader loggingReader;
Here is the new ConsoleCopy constructor:
public ConsoleCopy(string path)
{
oldOut = Console.Out;
oldIn = Console.In; // ADDED
try
{
fileStream = File.Create(path);
fileWriter = new StreamWriter(fileStream);
fileWriter.AutoFlush = true;
doubleWriter = new DoubleWriter(fileWriter, oldOut);
loggingReader = new LoggingReader(oldIn, fileWriter); // ADDED
}
catch (Exception e)
{
Console.WriteLine("Cannot open file for writing");
Console.WriteLine(e.Message);
return;
}
Console.SetOut(doubleWriter);
Console.SetIn(loggingReader); // ADDED
}
The log file contents:
This is output 1
This is output 2
> Test
Test command received
Enter to exit
>
After much googling and searching, I managed to send an image using multiparsers from android to my WCF service, but ideally, I'd like to send several images at once, instead of calling the method over and over again, since it'd take a lot longer, and add a bunch more overhead.
This is my current code
Android (Taken from code found on here somewhere):
public static String postFile(Bitmap bitmap, String urlString) throws Exception {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 30, bao);
byte[] data = bao.toByteArray();
//filename
String fileName = String.format("File_%d.png",new Date().getTime());
ByteArrayBody bab = new ByteArrayBody(data, fileName);
builder.addPart("image", bab);
final HttpEntity yourEntity = builder.build();
class ProgressiveEntity implements HttpEntity {
#Override
public void consumeContent() throws IOException {
yourEntity.consumeContent();
}
#Override
public InputStream getContent() throws IOException,
IllegalStateException {
return yourEntity.getContent();
}
#Override
public Header getContentEncoding() {
return yourEntity.getContentEncoding();
}
#Override
public long getContentLength() {
return yourEntity.getContentLength();
}
#Override
public Header getContentType() {
return yourEntity.getContentType();
}
#Override
public boolean isChunked() {
return yourEntity.isChunked();
}
#Override
public boolean isRepeatable() {
return yourEntity.isRepeatable();
}
#Override
public boolean isStreaming() {
return yourEntity.isStreaming();
} // CONSIDER put a _real_ delegator into here!
#Override
public void writeTo(OutputStream outstream) throws IOException {
class ProxyOutputStream extends FilterOutputStream {
/**
* #author Stephen Colebourne
*/
public ProxyOutputStream(OutputStream proxy) {
super(proxy);
}
public void write(int idx) throws IOException {
out.write(idx);
}
public void write(byte[] bts) throws IOException {
out.write(bts);
}
public void write(byte[] bts, int st, int end) throws IOException {
out.write(bts, st, end);
}
public void flush() throws IOException {
out.flush();
}
public void close() throws IOException {
out.close();
}
} // CONSIDER import this class (and risk more Jar File Hell)
class ProgressiveOutputStream extends ProxyOutputStream {
public ProgressiveOutputStream(OutputStream proxy) {
super(proxy);
}
public void write(byte[] bts, int st, int end) throws IOException {
// FIXME Put your progress bar stuff here!
out.write(bts, st, end);
}
}
yourEntity.writeTo(new ProgressiveOutputStream(outstream));
}
};
ProgressiveEntity myEntity = new ProgressiveEntity();
post.setEntity(myEntity);
HttpResponse response = client.execute(post);
return getContent(response);
}
public static String getContent(HttpResponse response) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = "";
String content = "";
while ((body = rd.readLine()) != null)
{
content += body + "\n";
}
return content.trim();
}
C# WCF Service method to take it
[WebInvoke(UriTemplate = "UploadPicture/{filename}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public String UploadPicture(string filename, Stream fileStream)
{
WriteLog("Uploading picture...");
try
{
MultipartParser parser = new MultipartParser(fileStream);
if (parser.Success)
{
string fileName = parser.Filename;
string contentType = parser.ContentType;
byte[] fileContent = parser.FileContents;
FileStream fileToupload = new FileStream("\\\\OHS-SUN\\Tracker\\robbie\\" + filename, FileMode.Create);
fileToupload.Write(fileContent, 0, fileContent.Length);
fileToupload.Close();
fileToupload.Dispose();
fileStream.Close();
return "Success !!!";
}
else
{
return "Exception!!!";
}
}
catch (Exception ex)
{
WriteLog("Uploading picture exception: " + ex.Message);
}
return "Picture uploaded!";
}
I'd like to go from sending one image, to sending several, each with 2 text attributes; the filename, and the project number they're associated with. Essentially, both is what I need it for. At the moment, I'm just trying to do put another addPart on to the android bit, but then I don't know how to add metadata to that and I wouldn't know how to parse it based on the name. I'm fine with using any third party libraries, the one I'm using on C# at the moment is already one.
Thanks a lot!
Instead of sending multiple images in one thing, I just stuck it in an asynchronous class and sent them concurrently with a max of 10 at a time until all the images are sent in that particular session. Seems to work fine, the implementation's the same, so I've not had to change any of that, which is good. If anyone would like me to post the code I did to do that, I'd be happy to. It's just small snippets here and there added to the above code, though.
Well, the main bit I added was:
public static class FileUploader extends AsyncTask<UploadFile , Void , String> implements Future<String>
{
#Override
protected void onPreExecute() {
filesUploading ++;
}
#Override
protected String doInBackground(UploadFile... uploadFile)
{
try
{
return postFile(uploadFile[0].file, uploadFile[0].projectNo, uploadFile[0].filename);
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
filesUploading --;
}
#Override
public boolean isDone() {
return AsyncTask.Status.FINISHED == getStatus();
}
}
This allows me to send each image separately, and handle them separately.
I have a program which outputs various results onto a command line console.
How do I save the output to a text file using a StreamReader or other techniques?
System.Collections.Generic.IEnumerable<String> lines = File.ReadAllLines(#"C:\Test\ntfs8.txt");
foreach (String r in lines.Skip(1))
{
String[] token = r.Split(',');
String[] datetime = token[0].Split(' ');
String timeText = datetime[4];
String actions = token[2];
Console.WriteLine("The time for this array is: " + timeText);
Console.WriteLine(token[7]);
Console.WriteLine(actions);
MacActions(actions);
x = 1;
Console.WriteLine("================================================");
}
if (x == 2)
{
Console.WriteLine("The selected time does not exist within the log files!");
}
System.IO.StreamReader reader = ;
string sRes = reader.ReadToEnd();
StreamWriter SW;
SW = File.CreateText("C:\\temp\\test.bodyfile");
SW.WriteLine(sRes);
SW.Close();
Console.WriteLine("File Created");
reader.Close();
Try this example from this article - Demonstrates redirecting the Console output to a file
using System;
using System.IO;
static public void Main ()
{
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;
try
{
ostrm = new FileStream ("./Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter (ostrm);
}
catch (Exception e)
{
Console.WriteLine ("Cannot open Redirect.txt for writing");
Console.WriteLine (e.Message);
return;
}
Console.SetOut (writer);
Console.WriteLine ("This is a line of text");
Console.WriteLine ("Everything written to Console.Write() or");
Console.WriteLine ("Console.WriteLine() will be written to a file");
Console.SetOut (oldOut);
writer.Close();
ostrm.Close();
Console.WriteLine ("Done");
}
Try if this works:
FileStream filestream = new FileStream("out.txt", FileMode.Create);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);
For the question:
How to save Console.Writeline Outputs
to text file?
I would use Console.SetOut as others have mentioned.
However, it looks more like you are keeping track of your program flow. I would consider using Debug or Trace for keeping track of the program state.
It works similar the console except you have more control over your input such as WriteLineIf.
Debug will only operate when in debug mode where as Trace will operate in both debug or release mode.
They both allow for listeners such as output files or the console.
TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(tr1);
TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText("Output.txt"));
Debug.Listeners.Add(tr2);
-http://support.microsoft.com/kb/815788
do you want to write code for that or just use command-line feature 'command redirection' as follows:
app.exe >> output.txt
as demonstrated here: http://discomoose.org/2006/05/01/output-redirection-to-a-file-from-the-windows-command-line/ (Archived at archive.org)
EDIT: link dead, here's another example: http://pcsupport.about.com/od/commandlinereference/a/redirect-command-output-to-file.htm
Based in the answer by WhoIsNinja:
This code will output both into the Console and into a Log string that can be saved into a file, either by appending lines to it or by overwriting it.
The default name for the log file is 'Log.txt' and is saved under the Application path.
public static class Logger
{
public static StringBuilder LogString = new StringBuilder();
public static void WriteLine(string str)
{
Console.WriteLine(str);
LogString.Append(str).Append(Environment.NewLine);
}
public static void Write(string str)
{
Console.Write(str);
LogString.Append(str);
}
public static void SaveLog(bool Append = false, string Path = "./Log.txt")
{
if (LogString != null && LogString.Length > 0)
{
if (Append)
{
using (StreamWriter file = System.IO.File.AppendText(Path))
{
file.Write(LogString.ToString());
file.Close();
file.Dispose();
}
}
else
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(Path))
{
file.Write(LogString.ToString());
file.Close();
file.Dispose();
}
}
}
}
}
Then you can use it like this:
Logger.WriteLine("==========================================================");
Logger.Write("Loading 'AttendPunch'".PadRight(35, '.'));
Logger.WriteLine("OK.");
Logger.SaveLog(true); //<- default 'false', 'true' Append the log to an existing file.
Create a class Logger(code below), replace Console.WriteLine with Logger.Out.
At the end write to a file the string Log
public static class Logger
{
public static StringBuilder LogString = new StringBuilder();
public static void Out(string str)
{
Console.WriteLine(str);
LogString.Append(str).Append(Environment.NewLine);
}
}
Use Console.SetOut to redirect to a TextWriter as described here:
http://msdn.microsoft.com/en-us/library/system.console.setout.aspx
Necromancing.
I usually just create a class, which I can wrap around main in an IDisposable.
So I can log the console output to a file without modifying the rest of the code.
That way, I have the output in both the console and for later reference in a text-file.
public class Program
{
public static async System.Threading.Tasks.Task Main(string[] args)
{
using (ConsoleOutputMultiplexer co = new ConsoleOutputMultiplexer())
{
// Do something here
System.Console.WriteLine("Hello Logfile and Console 1 !");
System.Console.WriteLine("Hello Logfile and Console 2 !");
System.Console.WriteLine("Hello Logfile and Console 3 !");
} // End Using co
System.Console.WriteLine(" --- Press any key to continue --- ");
System.Console.ReadKey();
await System.Threading.Tasks.Task.CompletedTask;
} // End Task Main
}
with
public class MultiTextWriter
: System.IO.TextWriter
{
protected System.Text.Encoding m_encoding;
protected System.Collections.Generic.IEnumerable<System.IO.TextWriter> m_writers;
public override System.Text.Encoding Encoding => this.m_encoding;
public override System.IFormatProvider FormatProvider
{
get
{
return base.FormatProvider;
}
}
public MultiTextWriter(System.Collections.Generic.IEnumerable<System.IO.TextWriter> textWriters, System.Text.Encoding encoding)
{
this.m_writers = textWriters;
this.m_encoding = encoding;
}
public MultiTextWriter(System.Collections.Generic.IEnumerable<System.IO.TextWriter> textWriters)
: this(textWriters, textWriters.GetEnumerator().Current.Encoding)
{ }
public MultiTextWriter(System.Text.Encoding enc, params System.IO.TextWriter[] textWriters)
: this((System.Collections.Generic.IEnumerable<System.IO.TextWriter>)textWriters, enc)
{ }
public MultiTextWriter(params System.IO.TextWriter[] textWriters)
: this((System.Collections.Generic.IEnumerable<System.IO.TextWriter>)textWriters)
{ }
public override void Flush()
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
thisWriter.Flush();
}
}
public async override System.Threading.Tasks.Task FlushAsync()
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
await thisWriter.FlushAsync();
}
await System.Threading.Tasks.Task.CompletedTask;
}
public override void Write(char[] buffer, int index, int count)
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
thisWriter.Write(buffer, index, count);
}
}
public override void Write(System.ReadOnlySpan<char> buffer)
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
thisWriter.Write(buffer);
}
}
public async override System.Threading.Tasks.Task WriteAsync(char[] buffer, int index, int count)
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
await thisWriter.WriteAsync(buffer, index, count);
}
await System.Threading.Tasks.Task.CompletedTask;
}
public async override System.Threading.Tasks.Task WriteAsync(System.ReadOnlyMemory<char> buffer, System.Threading.CancellationToken cancellationToken = default)
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
await thisWriter.WriteAsync(buffer, cancellationToken);
}
await System.Threading.Tasks.Task.CompletedTask;
}
protected override void Dispose(bool disposing)
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
thisWriter.Dispose();
}
}
public async override System.Threading.Tasks.ValueTask DisposeAsync()
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
await thisWriter.DisposeAsync();
}
await System.Threading.Tasks.Task.CompletedTask;
}
public override void Close()
{
foreach (System.IO.TextWriter thisWriter in this.m_writers)
{
thisWriter.Close();
}
} // End Sub Close
} // End Class MultiTextWriter
public class ConsoleOutputMultiplexer
: System.IDisposable
{
protected System.IO.TextWriter m_oldOut;
protected System.IO.FileStream m_logStream;
protected System.IO.StreamWriter m_logWriter;
protected MultiTextWriter m_multiPlexer;
public ConsoleOutputMultiplexer()
{
this.m_oldOut = System.Console.Out;
try
{
this.m_logStream = new System.IO.FileStream("./Redirect.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
this.m_logWriter = new System.IO.StreamWriter(this.m_logStream);
this.m_multiPlexer = new MultiTextWriter(this.m_oldOut.Encoding, this.m_oldOut, this.m_logWriter);
System.Console.SetOut(this.m_multiPlexer);
}
catch (System.Exception e)
{
System.Console.WriteLine("Cannot open Redirect.txt for writing");
System.Console.WriteLine(e.Message);
return;
}
} // End Constructor
void System.IDisposable.Dispose()
{
System.Console.SetOut(this.m_oldOut);
if (this.m_multiPlexer != null)
{
this.m_multiPlexer.Flush();
if (this.m_logStream != null)
this.m_logStream.Flush();
this.m_multiPlexer.Close();
}
if(this.m_logStream != null)
this.m_logStream.Close();
} // End Sub Dispose
} // End Class ConsoleOutputMultiplexer
Using only configuration in your app.config:
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener"/>
<!--
<add name="logListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
<add name="EventLogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="MyEventLog"/>
-->
<!--
Remove the Default listener to avoid duplicate messages
being sent to the debugger for display
-->
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
For testing, you can use DebugView before running the program, then we can easily view all of the log messages.
References:
http://blogs.msdn.com/b/jjameson/archive/2009/06/18/configuring-logging-in-a-console-application.aspx
http://www.thejoyofcode.com/from_zero_to_logging_with_system_diagnostics_in_15_minutes.aspx
Redirect Trace output to Console
Problem redirecting debug output to a file using trace listener
https://ukadcdiagnostics.codeplex.com/
http://geekswithblogs.net/theunstablemind/archive/2009/09/09/adventures-in-system.diagnostics.aspx
(If anything here needs clarification/ more detail please let me know.)
I have an application (C#, 2.* framework) that interfaces with a third-party webservice using SOAP. I used thinktecture's WSCF add-in against a supplied WSDL to create the client-side implementation. For reasons beyond my control the SOAP message exchange uses WSE2.0 for security (the thinctecture implementation had to be modified to include the WSE2.0 reference). In addition to the 'normal' data package I attach a stored X509 cert and a binary security token from a previous call to a different web service. We are using SSL encryption of some sort - I don't know the details.
All the necessary serialization/deserialization is contained in the web service client - meaning when control is returned to me after calling the client the entire XML string contained in the SOAP response is not available to me - just the deserialized components. Don't get me wrong - I think that's good because it means I don't have to do it myself.
However, in order for me to have something worth storing/archiving I am having to re-serialize the data at the root element. This seems like a waste of resources since my result was in the SOAP response.
Now for my question:
How can I get access to a 'clear' version of the SOAP response so that I don't have to re-serialize everything for storage/archiving?
Edit- My application is a 'formless' windows app running as a network service - triggered by a WebsphereMQ client trigger monitor. I don't think ASP.NET solutions will apply.
Edit - Since the consensus so far is that it doesn't matter whether my app is ASP.NET or not then I will give CodeMelt's (and by extension Chris's) solution a shot.
You can utilize SoapExtension from existing WSE2.0 framework to intercept the responses from the server.
public class MyClientSOAPExtension : SoapExtension
{
Stream oldStream;
Stream newStream;
// Save the Stream representing the SOAP request or SOAP response into
// a local memory buffer.
public override Stream ChainStream( Stream stream )
{
oldStream = stream;
newStream = new MemoryStream();
return newStream;
}
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeDeserialize:
// before the XML deserialized into object.
break;
case SoapMessageStage.AfterDeserialize:
break;
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
break;
default:
throw new Exception("Invalid stage...");
}
}
}
At stage of SoapMessageStage.BeforeDeserialize,
You can read the expected data you want from oldstream (e.g. use XmlReader).
Then store the expected data somewhere for yourself to use and also you need
forward the old stream data to the newstream for web service later stage to use the data, e.g. deserialize XML into objects.
The sample of logging all the traffic for the web service from MSDN
Here is an example you can setup using Visual studio web reference to http://footballpool.dataaccess.eu/data/info.wso?WSDL
Basically, you must insert in the webservice call chain a XmlReader spyer that will reconstruct the raw XML.
I believe this way is somehow simpler that using SoapExtensions.
Solution solution was inspired by http://orbinary.com/blog/2010/01/getting-the-raw-soap-xml-sent-via-soaphttpclientprotocol/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Reflection;
using System.Xml;
namespace ConsoleApplication1 {
public class XmlReaderSpy : XmlReader {
XmlReader _me;
public XmlReaderSpy(XmlReader parent) {
_me = parent;
}
/// <summary>
/// Extracted XML.
/// </summary>
public string Xml;
#region Abstract method that must be implemented
public override XmlNodeType NodeType {
get {
return _me.NodeType;
}
}
public override string LocalName {
get {
return _me.LocalName;
}
}
public override string NamespaceURI {
get {
return _me.NamespaceURI;
}
}
public override string Prefix {
get {
return _me.Prefix;
}
}
public override bool HasValue {
get { return _me.HasValue; }
}
public override string Value {
get { return _me.Value; }
}
public override int Depth {
get { return _me.Depth; }
}
public override string BaseURI {
get { return _me.BaseURI; }
}
public override bool IsEmptyElement {
get { return _me.IsEmptyElement; }
}
public override int AttributeCount {
get { return _me.AttributeCount; }
}
public override string GetAttribute(int i) {
return _me.GetAttribute(i);
}
public override string GetAttribute(string name) {
return _me.GetAttribute(name);
}
public override string GetAttribute(string name, string namespaceURI) {
return _me.GetAttribute(name, namespaceURI);
}
public override void MoveToAttribute(int i) {
_me.MoveToAttribute(i);
}
public override bool MoveToAttribute(string name) {
return _me.MoveToAttribute(name);
}
public override bool MoveToAttribute(string name, string ns) {
return _me.MoveToAttribute(name, ns);
}
public override bool MoveToFirstAttribute() {
return _me.MoveToFirstAttribute();
}
public override bool MoveToNextAttribute() {
return _me.MoveToNextAttribute();
}
public override bool MoveToElement() {
return _me.MoveToElement();
}
public override bool ReadAttributeValue() {
return _me.ReadAttributeValue();
}
public override bool Read() {
bool res = _me.Read();
Xml += StringView();
return res;
}
public override bool EOF {
get { return _me.EOF; }
}
public override void Close() {
_me.Close();
}
public override ReadState ReadState {
get { return _me.ReadState; }
}
public override XmlNameTable NameTable {
get { return _me.NameTable; }
}
public override string LookupNamespace(string prefix) {
return _me.LookupNamespace(prefix);
}
public override void ResolveEntity() {
_me.ResolveEntity();
}
#endregion
protected string StringView() {
string result = "";
if (_me.NodeType == XmlNodeType.Element) {
result = "<" + _me.Name;
if (_me.HasAttributes) {
_me.MoveToFirstAttribute();
do {
result += " " + _me.Name + "=\"" + _me.Value + "\"";
} while (_me.MoveToNextAttribute());
//Let's put cursor back to Element to avoid messing up reader state.
_me.MoveToElement();
}
if (_me.IsEmptyElement) {
result += "/";
}
result += ">";
}
if (_me.NodeType == XmlNodeType.EndElement) {
result = "</" + _me.Name + ">";
}
if (_me.NodeType == XmlNodeType.Text || _me.NodeType == XmlNodeType.Whitespace) {
result = _me.Value;
}
if (_me.NodeType == XmlNodeType.XmlDeclaration) {
result = "<?" + _me.Name + " " + _me.Value + "?>";
}
return result;
}
}
public class MyInfo : ConsoleApplication1.eu.dataaccess.footballpool.Info {
protected XmlReaderSpy _xmlReaderSpy;
public string Xml {
get {
if (_xmlReaderSpy != null) {
return _xmlReaderSpy.Xml;
}
else {
return "";
}
}
}
protected override XmlReader GetReaderForMessage(System.Web.Services.Protocols.SoapClientMessage message, int bufferSize) {
XmlReader rdr = base.GetReaderForMessage(message, bufferSize);
_xmlReaderSpy = new XmlReaderSpy((XmlReader)rdr);
return _xmlReaderSpy;
}
}
class Program {
static void Main(string[] args) {
MyInfo info = new MyInfo();
string[] rest = info.Cities();
System.Console.WriteLine("RAW Soap XML response :\n"+info.Xml);
System.Console.ReadLine();
}
}
}
Old thread, but in case others are looking to do this today: these ideas of leveraging SoapExtension or creating 'spy' classes are great, but don't work in .NET Core.
#mting923's suggestion to use IClientMessageInspector approach works in .NET Core 3.1; see here: Get SOAP Message before sending it to the WebService in .NET.
A generated SOAP proxy class is still just a WCF client under the hood, and so the IClientMessageInspector approach works a treat, even for an .NET Core Azure Function calling an older SOAP web service. The following works for me in a .NET Core 3.1 Azure Function:
public class SoapMessageInspector : IClientMessageInspector
{
public string LastRequestXml { get; private set; }
public string LastResponseXml { get; private set; }
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
LastRequestXml = request.ToString();
return request;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
LastResponseXml = reply.ToString();
}
}
public class SoapInspectorBehavior : IEndpointBehavior
{
private readonly SoapMessageInspector inspector_ = new SoapMessageInspector();
public string LastRequestXml => inspector_.LastRequestXml;
public string LastResponseXml => inspector_.LastResponseXml;
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(inspector_);
}
}
And then it can be set up like this:
var client = new ServiceClient();
var soapInspector = new SoapInspectorBehavior();
client.Endpoint.EndpointBehaviors.Add(soapInspector);
After invoking a web service call on the client proxy, soapInspector.LastRequestXml and soapInspector.LastResponseXml will contain the raw SOAP request and response (as strings).
Inspired by jfburdet, I wanted to see if it was possible to directly intercept at stream/byte level rather than reconstructing XML. And it is! See code below:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using System.Xml;
using Test.MyWebReference;
namespace Test {
/// <summary>
/// Adds the ability to retrieve the SOAP request/response.
/// </summary>
public class ServiceSpy : OriginalService {
private StreamSpy writerStreamSpy;
private XmlTextWriter xmlWriter;
private StreamSpy readerStreamSpy;
private XmlTextReader xmlReader;
public MemoryStream WriterStream {
get { return writerStreamSpy == null ? null : writerStreamSpy.ClonedStream; }
}
public XmlTextWriter XmlWriter {
get { return xmlWriter; }
}
public MemoryStream ReaderStream {
get { return readerStreamSpy == null ? null : readerStreamSpy.ClonedStream; }
}
public XmlTextReader XmlReader {
get { return xmlReader; }
}
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
DisposeWriterStreamSpy();
DisposeReaderStreamSpy();
}
protected override XmlWriter GetWriterForMessage(SoapClientMessage message, int bufferSize) {
// Dispose previous writer stream spy.
DisposeWriterStreamSpy();
writerStreamSpy = new StreamSpy(message.Stream);
// XML should always support UTF8.
xmlWriter = new XmlTextWriter(writerStreamSpy, Encoding.UTF8);
return xmlWriter;
}
protected override XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize) {
// Dispose previous reader stream spy.
DisposeReaderStreamSpy();
readerStreamSpy = new StreamSpy(message.Stream);
xmlReader = new XmlTextReader(readerStreamSpy);
return xmlReader;
}
private void DisposeWriterStreamSpy() {
if (writerStreamSpy != null) {
writerStreamSpy.Dispose();
writerStreamSpy.ClonedStream.Dispose();
writerStreamSpy = null;
}
}
private void DisposeReaderStreamSpy() {
if (readerStreamSpy != null) {
readerStreamSpy.Dispose();
readerStreamSpy.ClonedStream.Dispose();
readerStreamSpy = null;
}
}
/// <summary>
/// Wrapper class to clone read/write bytes.
/// </summary>
public class StreamSpy : Stream {
private Stream wrappedStream;
private long startPosition;
private MemoryStream clonedStream = new MemoryStream();
public StreamSpy(Stream wrappedStream) {
this.wrappedStream = wrappedStream;
startPosition = wrappedStream.Position;
}
public MemoryStream ClonedStream {
get { return clonedStream; }
}
public override bool CanRead {
get { return wrappedStream.CanRead; }
}
public override bool CanSeek {
get { return wrappedStream.CanSeek; }
}
public override bool CanWrite {
get { return wrappedStream.CanWrite; }
}
public override void Flush() {
wrappedStream.Flush();
}
public override long Length {
get { return wrappedStream.Length; }
}
public override long Position {
get { return wrappedStream.Position; }
set { wrappedStream.Position = value; }
}
public override int Read(byte[] buffer, int offset, int count) {
long relativeOffset = wrappedStream.Position - startPosition;
int result = wrappedStream.Read(buffer, offset, count);
if (clonedStream.Position != relativeOffset) {
clonedStream.Position = relativeOffset;
}
clonedStream.Write(buffer, offset, result);
return result;
}
public override long Seek(long offset, SeekOrigin origin) {
return wrappedStream.Seek(offset, origin);
}
public override void SetLength(long value) {
wrappedStream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count) {
long relativeOffset = wrappedStream.Position - startPosition;
wrappedStream.Write(buffer, offset, count);
if (clonedStream.Position != relativeOffset) {
clonedStream.Position = relativeOffset;
}
clonedStream.Write(buffer, offset, count);
}
public override void Close() {
wrappedStream.Close();
base.Close();
}
protected override void Dispose(bool disposing) {
if (wrappedStream != null) {
wrappedStream.Dispose();
wrappedStream = null;
}
base.Dispose(disposing);
}
}
}
}
The MSDN Library includes example code for obtaining the XML of both the request and the response that you can use to archive it. Obviously you'll have to make some changes since the example stores data in a text file, but it isn't too complicated.