Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6701999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:00:04+00:00 2026-05-26T07:00:04+00:00

I am trying to log some XML responses from a WCF Service using log4net.

  • 0

I am trying to log some XML responses from a WCF Service using log4net.

I want the output of the XML file to the log to be in properly formed XML. The request comes in as an XMLElement.

Example:

The request comes in as this:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationEvent xmlns="http://courts.wa.gov/INH_TV/ApplicationEvent.xsd">
  <Severity xmlns="">Information</Severity>
  <Application xmlns="">Application1</Application>
  <Category xmlns="">Timings</Category>
  <EventID xmlns="">1000</EventID>
  <DateTime xmlns="">2012-09-02T12:05:15.234Z</DateTime>
  <MachineName xmlns="">Server1</MachineName>
  <MessageID xmlns="">10000000-0000-0000-0000-000000000000</MessageID>
  <Program xmlns="">Progam1</Program>
  <Action xmlns="">Entry</Action>
  <UserID xmlns="">User1</UserID>
</ApplicationEvent>

Then if I output this value to log4net.

logger.Info(request.OuterXml);

I get the entire document logged in a single line like so:

<ApplicationEvent xmlns="http://courts.wa.gov/INH_TV/ApplicationEvent.xsd"><Severity xmlns="">Information</Severity><Application xmlns="">Application1</Application><Category xmlns="">Timings</Category><EventID xmlns="">1000</EventID><DateTime xmlns="">2012-09-02T12:05:15.234Z</DateTime><MachineName xmlns="">Server1</MachineName><MessageID xmlns="">10000000-0000-0000-0000-000000000000</MessageID><Program xmlns="">Progam1</Program><Action xmlns="">Entry</Action><UserID xmlns="">User1</UserID></ApplicationEvent>

I would like it to display in the log.txt file formatted correctly as it came in. So far the only way I have found to do this is to convert it to an XElement like so:

XmlDocument logXML = new XmlDocument();
logXML.AppendChild(logXML.ImportNode(request, true));
XElement logMe = XElement.Parse(logXML.InnerXml);
logger.Info(logMe.ToString());

This doesn’t seem like good programming to me. I have been searching the documentation and I can’t find a built-in way to output this correctly without converting it.

Is there an obvious, better way that I am just missing?

edit1: Removed ToString() since OuterXML is a String value.

edit2: I answered my own question:

So I did some more research, and I guess I missed a piece of code in the documentation.

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.outerxml.aspx

I have it down to:

using (MemoryStream ms = new MemoryStream())
        {
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Indent = true;
            using (XmlWriter xmlWriter = XmlWriter.Create(ms, xws))
            {
                request.WriteTo(xmlWriter);
            }
            ms.Position = 0; StreamReader sr = new StreamReader(ms);
            string s = sr.ReadToEnd(); // s will contain indented xml
            logger.Info(s);
        }

Which is a little more efficient than my current method despite being more verbose.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:00:04+00:00Added an answer on May 26, 2026 at 7:00 am

    XElement parse is the cleanest way. You can save a line or two with:

    logger.Info(XElement.Parse(request.OuterXml).ToString()); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to disable log output from all external libraries in logback-test.xml. Somehow it
I'm trying to add some contacts from an xml file which have been serialize
I am trying to disable log rotation, for file handler using, FileHandler fh =
I am trying to read tags from XML using LibXML. I can print all
I'm using a Java program to POST some XML data from a remote client
I'm facing a problem when trying to log my application using log4net. My application
I am trying to log some input values into an array via jquery and
I am trying to use the Event Log to write some debug information and
I am trying to log a method's parameters by using reflection. I read the
Using Forms Authentication in ASP.NET MVC when trying to log back into a site,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.