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 6201135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:29:56+00:00 2026-05-24T04:29:56+00:00

I’m building a web UI to help automate our deployment process and am going

  • 0

I’m building a web UI to help automate our deployment process and am going to write a powershell script to do the deployment and would like it’s Write-Debug (or any statement to log, just let me know which to use 🙂 ) statements to be logged to the deployed package‘s database variable Log. I haven’t really used log4net before so please don’t laugh if I’m doing this completely wrong.

I figure since the location is dynamic, I’d have to code the log4net appenders, but would it be easier/better to do all of the log4net stuff inside of the powershell script? I read this and found I should use ps.Streams.Debug.DataAdded += new EventHandler<DataAddedEventArgs>(delegate(object sender, DataAddedEventArgs e) to get the write-debug information.

Here is what I have so far:

public static void Test(Package pkg)
    {
        //Do roll_out
        //Creates a cmd prompt
        PowerShell ps = PowerShell.Create();
        string myCommand = @"C:\Users\evan.layman\Desktop\test.ps1";

    ps.AddCommand(myCommand);

    ps.Streams.Debug.DataAdded += new EventHandler<DataAddedEventArgs>(delegate(object sender, DataAddedEventArgs e)
    {
        PSDataCollection<DebugRecord> debugStream = (PSDataCollection<DebugRecord>)sender;
        DebugRecord record = debugStream[e.Index];

        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
        hierarchy.Root.RemoveAllAppenders(); /*Remove any other appenders*/

        AdoNetAppender appender = new AdoNetAppender();
        appender.ConnectionString = ConfigurationManager.ConnectionStrings["DeploymentConnectionString"].ConnectionString;
        appender.CommandText = "with cte as (SELECT * FROM Package PackageID =" + pkg.PackageID + ") UPDATE cte SET (Log) VALUES (?logText)";
        AdoNetAppenderParameter param = new AdoNetAppenderParameter();
        param.DbType = System.Data.DbType.String;
        param.ParameterName = "logText";
        param.Layout = new log4net.Layout.RawTimeStampLayout();
        appender.AddParameter(param);
        BasicConfigurator.Configure(appender);

        ILog log = LogManager.GetLogger("PowerShell");
        log.Debug(record.Message);
        //log.DebugFormat("{0}:{1}", DateTime.UtcNow, record);
        //log.Warn(record, new Exception("Log failed"));
    });
    Collection<PSObject> commandResults = ps.Invoke();

Hopefully I can get this working 🙂

  • 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-24T04:29:56+00:00Added an answer on May 24, 2026 at 4:29 am

    I would keep as much log4net config out of your code as possible. In your code, the config is being recreated on each debug statement, which is inefficient.

    It’s possible to do what you want using event context properties in log4net. I’ve blogged about log4net event context a bit on my blog.

    Here’s a quick example that’s close to your existing codebase….

    This C# code shows how to use log4net global properties to store custom event context data; note the setting of the “PackageID” global property value before the pipeline is executed…

    using System;
    using System.Management.Automation;
    using log4net;
    
    // load log4net configuration from app.config
    [assembly:log4net.Config.XmlConfigurator]
    
    namespace ConsoleApplication1
    {
        class Program
        {
            private static PowerShell _ps;
            private static ILog Log = log4net.LogManager.GetLogger(typeof (Program));
    
            static void Main(string[] args)
            {
                string script = "write-debug 'this is a debug string' -debug";
    
                for (int packageId = 1; packageId <= 5; ++packageId)
                {
                    using (_ps = PowerShell.Create())
                    {
                        _ps.Commands.AddScript(script);
                        _ps.Streams.Debug.DataAdded += WriteDebugLog;
    
                        // set the PackageID global log4net property 
                        log4net.GlobalContext.Properties["PackageID"] = packageId;
    
                        // sync invoke your pipeline
                        _ps.Invoke();
    
                        // clear the PackageID global log4net property 
                        log4net.GlobalContext.Properties["PackageID"] = null;
                    }
                }        
            }
    
            private static void WriteDebugLog(object sender, DataAddedEventArgs e)
            {
                // get the debug record and log the message
                var record = _ps.Streams.Debug[e.Index];
                Log.Debug(record.Message);            
            }
        }
    }
    

    And here is the app.config that drops the logs into the database; note the custom PackageID parameter in the SQL, and how the value is pulled from the log4net property stack:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>
    <log4net>
      <appender name="Ado" type="log4net.Appender.AdoNetAppender">
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <connectionString value="data source=vbox-xp-sql;initial catalog=test1;integrated security=false;persist security info=True;User ID=test1;Password=password" />
        <commandText value="INSERT INTO Log ([Message],[PackageID]) VALUES (@message, @packageid)" />
        <parameter>
          <parameterName value="@message" />
          <dbType value="String" />
          <size value="4000" />
          <layout type="log4net.Layout.PatternLayout" value="%message" />
        </parameter>
        <parameter>
          <parameterName value="@packageid" />
          <dbType value="Int32" />
          <size value="4" />
          <!-- use the current value of the PackageID property -->
          <layout type="log4net.Layout.PatternLayout" value="%property{PackageID}" />
        </parameter>
      </appender>
    
      <root>
        <level value="ALL" />
        <appender-ref ref="Ado" />
      </root> 
    </log4net>
    </configuration>
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.