desharp

2025-12-10 0 722

Desharp – C# & VB .NET Debugging Tool


Outline

  • About
  • Instalation
  • Demos & Examples
  • Do Not Miss
  • Usage In Code

    • Basic Dumping & Logging Any Structuralized Variables
    • Basic Dumping & Logging Exceptions
    • All Dump Methods
    • All Log Methods
    • All Other Methods
  • Dumps & Logs Outputs
  • What You Can Dump Or Log
  • What Is Rendered (Dumps & Logs)
  • Configuration

    • Configuration Examples
    • Runtime Configuration Options
    • All XML Configuration Options
  • Visual Studio – Code Snippets

About

C# & VB .NET debugging utility for:

  • dump or log any structuralized variables
  • dump or log exceptions with stack trace and inner exceptions

Dump any variables into:

  • Console window
  • Output debug panel in Visual Studio
  • Web response in floating window
  • Html/text log files on HDD
  • Any WinForms or WPF field component

Instalation

Desharp on Nuget.org

PM> Install-Package Desharp

Demos & Examples

  • Console Application Demo (C#), (VB.NET)
    Demo dumps and exceptions rendering into console window, logging on HDD and optional tests running.
  • Windows Forms Application Demo (C#)
    Demo dumps and exceptions rendering into text field component, into debug output window and logging on HDD.
  • Web Basic Application Demo (C#)
    Demo dumps and exceptions rendering into floating browser bar and logging on HDD.
  • Web MVC Application Demo (C#)
    Demo dumps and exceptions rendering into floating browser bar and logging on HDD.
  • Web Forms Application Demo (C#)
    Demo dumps and exceptions rendering into floating browser bar and logging on HDD.

Do Not Miss

  • Visual Studio code snippets
    Download and install predefined VS snippets for most offten Desharp calls.
  • Visual Studio opener
    Automatic Visual Studio (or any other) editor opening on specific file and line from rendered logs and exceptions.

Usage In Code

Basic Dumping & Logging Any Structuralized Variables

C# Basic Example

using Desharp;
using System.Collections.Generic;

var list = new List<int?>() { 100, 200, null };
Debug.Dump(list);  // print list by Console.WriteLine(); or append into html response as floating window
Debug.Log(list);   // store dumped list in debug.log or debug.html file on HDD

VB Basic Example

Imports Desharp
Imports System.Collections.Generic

Dim list As New List(Of Int32?)() { 100, 200, null }
Debug.Dump(list)  \' print list by Console.WriteLine(); or append into html response as floating window
Debug.Log(list)   \' store dumped list in debug.log or debug.html file on HDD

Dumped result for both languages:

[List<Int32?>[3]]
   0: 100 [Int32]
   1: 200 [Int32]
   2: null

Basic Dumping & Logging Exceptions

C# Basic Example

try {
   throw new Exception(\"Something wrong!\");
} catch (Exception e) {
   Debug.Dump(e);  // print exception by Console.WriteLine(); or append into html response as floating window
   Debug.Log(e);   // store dumped exception in exception.log or exception.html file on HDD
}

VB Basic Example

Try
   Throw New Exception(\"Something wrong!\")
Catch e As Exception
   Debug.Dump(e)  \' print exception by Console.WriteLine(); or append into html response as floating window
   Debug.Log(e)   \' store dumped exception in exception.log or exception.html file on HDD
End Try

Dumped result for both languages:

System.Exception (Hash Code: 50632145):
   Message   : Something wrong!
   Time      : 2017-06-10 13:18:07:300
   Process ID: 7972
   Thread ID : 1
   File      : /Program.cs:8
   -------
       4 | namespace ExampleConsole {
       5 |     class Program {
       6 |         static void Main(string[] args) {
       7 |             try {
   ->  8 |                 throw new Exception(\"Something wrong!\");
       9 |             } catch (Exception e) {
      10 |                 Debug.Dump(e);  // print exception by Console.WriteLine(); or append into html response as floating window
      11 |                 Debug.Log(e);   // store dumped exception in exception.log or exception.html file on HDD
      12 |             }
   -------
   Callstack: 
      ExampleConsole.Program.Main(String[] args) /Program.cs 8

All Dump Methods

/**
 * Dump any values to application output (in web applications into debug bar,  
 * in desktop applications into console or debug output window)
 */
Desharp.Debug.Dump(params object[] args);

/**
 * Dump exception instance to application output if output dumping is enabled. It renders:
 * - exception type, exception message and exception hash id
 * - yes/no if exception has been caught or not caught
 * - error file where exception has been thrown
 * - thread call stack
 * All inner exceptions after this exception in the same way.
 */
Desharp.Debug.Dump(Exception exception = null, DumpOptions? options = default(DumpOptions?));

/**
 * Dump any values to application output (in web applications into debug bar,  
 * in desktop applications into console or debug output window)
 * This method dumps only single object with dump options like to dump
 * different object depth as usual, different string length or to dump source location and more...
 */
Desharp.Debug.Dump(object obj, DumpOptions? options = default(DumpOptions?));

/**
 * Dump any type value to direct application output (not into web request debug
 * bar in web applications!) and stop request/thread (in web applications dump into
 * direct response body, in desktop applications into console or debug output window).
 */
Desharp.Debug.DumpAndDie(object obj = null, DumpOptions? options = default(DumpOptions?));

All Log Methods

/**
 * Log exception instance as dumped string into exceptions.log|exceptions.html file. It stores:
 * - exception type
 * - exception message
 * - if exception has been caught or not caught
 * - exception hash id
 * - error file where exception has been thrown
 * - thread call stack
 * All inner exceptions after this exception in the same way.
 */
Desharp.Debug.Log(Exception exception = null);

/**
 * Log any type value to application *.log|*.html file, specified by level param.
 */
Desharp.Debug.Log(object obj = null, Level level = Level.INFO, int maxDepth = 0, int maxLength = 0);

All Other Methods

/**
 * Print to application output or log into file (if enabled) first param to be true
 * or not and describe what was equal or not in first param by second param message.
 */
Desharp.Debug.Assert(bool assertion, string description = \"\", Level logLevel = Level.DEBUG);

/**
 * Configure Desharp assembly from running application environment and override
 * any XML config settings or automatically detected settings.
 */
Desharp.Debug.Configure(DebugConfig cfg);

/**
 * Enable or disable variables dumping from application code environment for all threads 
 * or get enabled/disabled Desharp dumping state if no boolean param provided.
 */
Desharp.Debug.Enabled(bool? enabled = default(bool?));

/**
 * Return last uncaught Exception in request, mostly used in web applications by
 * error page rendering process to know something about Exception before.
 */
Desharp.Debug.GetLastError();

/**
 * Return spent request processing time for web applications or return application
 * up time for all other platforms.
 */
Desharp.Debug.GetProcessingTime();

/**
 * Print current thread stack trace into application output and exit running application.
 * In web applications - stop current request, in any other applications - stop application
 * with all it\'s threads and exit.
 */
Desharp.Debug.Stop();

/**
 * Prints to output or into log file number of seconds from last timer call under
 * called name in seconds with 3 floating point decimal spaces.
 * If no name specified or name is empty string, there is returned:
 * Web applications - number of seconds from request beginning.
 * Desktop applications - number of seconds from application start.
 */
Desharp.Debug.Timer(string name = null, bool returnTimerSeconds = false, Level logLevel = Level.DEBUG);

Dumps & Logs Outputs

  • console window for console applications
  • Visual Studio console for Windows Forms or WPF applications
  • floating window in html response for web applications
  • special HTTP headers for FilePHP browser extension for web applications
  • file logs in text/html formats

What You Can Dump Or Log

  • any variables
    • primitive variables and it\’s primitive arrays like: char, int?[] and more
    • collections: Lists, Dictionaries or any collections (IList, IDictionary, ICollection, IEnumerable…)
    • database results: DataSet, DataTable and DataRow with values
    • formated instances: DateTimeOffset, DateTime, TimeSpan, Guid, StringBuilder
    • any custom class instances with rendered events targets, properties values and fields values
    • anonymous objects like: new { any = \"value\" }
    • Func<> and Delegate types
    • reflection objects are only displayed as type names
  • exceptions
  • exceptions with inner exceptions
  • much more… you can try:-)

What Is Rendered (Dumps & Logs)

  • variables

    • always is rendered/logged the dumped variable:-)
    • dump is rendered with configurable return flag (to return dumped result as string)
    • dump or log is rendered with configurable:
      • source location call (default: false)
      • dump depth (default: 3)
      • maximum string length (default: 1024)
  • exceptions

    • dump or log has always rendered
      • exception message, process and thread id
      • file and line, where exception has been dumped, logged or thrown
        (only if source code is possible to target by *.PDB file, there is rendered
        a few lines from source code, where the Debug.dump() or Debug.Log()
        has been called or where Exception has been thrown)
      • exception callstack
        (with editor opener links in web dump or in html log)
      • all inner exceptions inside the given exception
    • exceptions in web environment has always rendered:
      • requested URL, all http request headers and client IP
      • all loaded assemblies
      • there are different dump rendering for catched and not catched exceptions:
        • if optional flag for catched exception is true, exception is rendered as
          openable exception in floating bar in left bottom screen corner
        • if optional flag for catched exception is false, exception is rendered
          over whole browser window immediately with possibility to close it back into floating bar

Configuration

You can configure the Desharp utility by:

  • app.config or web.config – more in all XML configuration options
  • anytime directly by calling Debug.Configure() from any thread – more in runtime configuration options

You can configure:

  • if dumping/debugging is enabled or not (enabled by default after nuget package installation)
  • debug IPs to enable debugging only for listed client IPs (localhost IPs by default)
  • logs directory (~/Logs by default, relative from app root)
  • logs file format:
    • html (*.html, by default in config file for all apps)
    • text (*.log, by default for desktop apps if config file is missing)
  • logging levels:
    • debug (by default for all variables)
    • exception (by default for logged exceptions)
    • info
    • notice
    • warning
    • error
    • critical
    • alert
    • emergency
    • javascript
  • favourite editor to open files from html debug output or log file by editor:// protocol (MSVS by default)
  • source file location to render where the Desharp library has been called in your source code

Configuration Examples

After instaling Desharp nuget package, there are automatically added subnodes
into your App.config or into Web.config file (if exists):

  • into node <appSettings> – for desktop and web applications
  • into node <system.webServer> – for web applications only

Console Apps, WinForms & WPF Apps:

<appSettings>
  ...
  <add key=\"Desharp:Enabled\" value=\"1\" />
  <add key=\"Desharp:Output\" value=\"html\" />
  <add key=\"Desharp:Levels\" value=\"exception,debug,info,-notice,-warning,error,critical,alert,emergency,javascript\" />
  <add key=\"Desharp:Directory\" value=\"~/Logs\" />
</appSettings>

Website Apps:

<appSettings>
  ...
  <add key=\"Desharp:Enabled\" value=\"1\" />
  <add key=\"Desharp:Output\" value=\"html\" />
  <add key=\"Desharp:DebugIps\" value=\"127.0.0.1,::1\" />`
  <add key=\"Desharp:Levels\" value=\"exception,debug,info,-notice,-warning,error,critical,alert,emergency,javascript\" />
  <add key=\"Desharp:Panels\" value=\"Desharp.Panels.SystemInfo,Desharp.Panels.Session\" />
  <add key=\"Desharp:Directory\" value=\"~/Logs\" />
</appSettings>
...
<system.webServer>
  ...
  <modules>
    ...
    <add name=\"Desharp\" type=\"Desharp.Module\" preCondition=\"managedHandler\" />
  </modules>
  <httpErrors errorMode=\"Detailed\" />
  ...
</system.webServer>

Runtime Configuration Options

To overwrite XML config settings in running application or to temporary define different configuration,
you can in any application place and in any thread to reconfigure Desharp utility (for all threads) to call:

Desharp.Debug.Configure(new Desharp.DebugConfig {
   Enabled = true,
   SourceLocation = true,
   Directory = \"~/Logs\",
   LogWriteMilisecond = 10000,
   Depth = 3,
   // `EnvType.Web` or `EnvType.Windows`, used very rarely:
   EnvType = EnvType.Web,
   // `Desharp.LogFormat.Html` or `Desharp.LogFormat.Text`:
   LogFormat = Desharp.LogFormat.Html,
   // for web apps only:
   ErrorPage = \"~/custom-error-page.html\",
   Panels = new[] { typeof(Desharp.Panels.SystemInfo), typeof(Desharp.Panels.Session) }
});

All XML Configuration Options

After instaling Desharp nuget package, there are automatically added
new xml file into your project root directory with name Desharp.config.example,
where are all detailed configuration options you can copy and paste:

<system.webServer>
<modules>
<add name="Desharp" type="Desharp.Module" preCondition="managedHandler" />
</modules>
<httpErrors errorMode="Detailed" />
</system.webServer>

<appSettings>


<add key="Desharp:Enabled" value="1" />


<add key="Desharp:Output" value="html" />


<add key="Desharp:DebugIps" value="127.0.0.1,::1" />


<add key="Desharp:Levels" value="+exception,debug,info,-notice,-warning,+error,+critical,alert,+emergency,javascript" />


<add key="Desharp:NotifySettings" value="{
host: \’smtp.company.com\’,
port: 587,
ssl: true,
user: \’noreply@company.com\’,
password: \’your-secret-password\’,
from: \’noreply@company.com\’,
to: \’your.name@gmail.com\’,
priority: \’high\’,
timeout: 30000
}" />


<add key="Desharp:Panels" value="Desharp.Panels.SystemInfo,Desharp.Panels.Session" />


<add key="Desharp:Directory" value="~/Logs" />


<add key="Desharp:SourceLocation" value="1" />


<add key="Desharp:WriteMiliseconds" value="5000" />


<add key="Desharp:Depth" value="3" />


<add key="Desharp:MaxLength" value="1024" />


<add key="Desharp:ErrorPage" value="~/custom-error-page-500.html" />


<add key="Desharp:DumpCompillerGenerated" value="true" />


<add key="Desharp:Editor" value="MSVS2019" />

</appSettings>
</configuration>\”>

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<configuration>
  
  <startup>
    <supportedRuntime version=\"v4.0\" sku=\".NETFramework,Version=v4.5\" />
  </startup>
  
  <!-- For web applications only - automatically added after nuget package installation: -->
  <system.webServer>
    <modules>
      <add name=\"Desharp\" type=\"Desharp.Module\" preCondition=\"managedHandler\" />
    </modules>
    <httpErrors errorMode=\"Detailed\" />
  </system.webServer>
  
  <appSettings>
    
    <!-- 
      If `true`, all dumps by method `Debug.Dump()` are internally rendered and displayed.
      For web applications, there is debug panel is displayed in browser with dumped variables or rendered exceptions.
      For desktop applications, there is console priniting enabled or output debug window printing enabled.
      - Not required to configure, but very recomanded.
      - Possible values: `1`, `0`, `true`, `false`.
      - If not configured - enabled is only when VS debugger is attached or entry assembly is builded as Debug.
      - If disabled - all `Debug.Log()` calls are still enabled and executed, see more option `Desharp:Levels`.
    -->
    <add key=\"Desharp:Enabled\" value=\"1\" />
    
    <!--
      Logs content format.
      - Not required, `text` by default.
      - Possible values: `html`, `text`.
    -->
    <add key=\"Desharp:Output\" value=\"html\" />
    
    <!--
      Client IP adresses list to limit `Desharp` enabled state only for some clients.
      - Not required to configure, very recomanded for web applications.
      - Possible values: IPv4 or IPv6, separated by comma (IPv6 without `[]` brackets).
      - If not configured and `Desharp` is in enabled state, then `Desharp` is enabled for all clients.
    -->
    <add key=\"Desharp:DebugIps\" value=\"127.0.0.1,::1\" />
    
    <!--
      Loggin levels to enable/disable to write on hard drive and also possibility to enable/disable email notifications.
      - Not required, recomanded.
      - Possible values: `exception`, `debug`, `info`, `notice`, `warning`, `error`, `critical`, `alert`, `emergency`, `javascript`.
      - If not configured, all logging levels are enabled for logging and not enabled for email notifications.
      - If at least one level is configured, then all other configured levels are disabled for logging and for email notifications.
      - If you want to enable any logging level - put the level name into node `value` attribute (comma separated).
      - If you want to disable any logging level - put minus (-) character before level name or remove level name.
      - If you want to enable any logging level for email notifications - put plus (+) character before level name.
		For any notification type with plus sign, it required to configure `Desharp:NotifySettings` property!
    -->
    <add key=\"Desharp:Levels\" value=\"+exception,debug,info,-notice,-warning,+error,+critical,alert,+emergency,javascript\" />

    <!--
      Logged messages notifications by configured levels.
      - Not required, recomanded in production mode.
      - Possible values: 
        - `host`: Required, mail server smtp domain | IPv4 | IPv6.
        - `port`: Not required, `25` by default.
        - `ssl`: Not required, `false` by default.
        - `from`: Required if no username and password specified, email address to specify sender, if no value specified, there is used `username` value.
        - `username` and `password`: Required if no `from` sender specified, mail server username/password credentials for sender account, always necessary to use together.
		- `domain` - Not required, no default value. Used only as third parametter for `System.Net.NetworkCredential` if presented.
        - `to`: Required, single recepient email adress or multiple adresses separated by semicolon `;`.
        - `priority`: Not required, possible values: `low` | `normal` | `high` (`normal` by defaut).
        - `timeout`: Not required, smtp server timeout specified in miliseconds, `10000` by default (10 seconds).
		- `background`: Not required at all. Default value is `true` to send all notifications in background thread. 
		  Use `false` value only to debug email sending.
    -->
    <add key=\"Desharp:NotifySettings\" value=\"{
		host: \'smtp.company.com\',
		port: 587,
		ssl: true,
		user: \'noreply@company.com\',
		password: \'your-secret-password\',
		from: \'noreply@company.com\',
		to: \'your.name@gmail.com\',
		priority: \'high\',
		timeout: 30000
    }\" />
    
    <!--
      Web debug bar panels.
      - Not required, recomanded.
      - Full class names separated by comma `,`.
      - Panel class has to implement public interface: `Desharp.Panels.IPanel`
      - Panel class could implement interface: `Desharp.Panels.ISessionPanel`,
        where are method called when session is is read and written every request.
      - There are always enabled build-in panels for execution time, dumps and exceptions.
      - Build-in panels you can optionally use: 
        - `Desharp.Panels.SystemInfo` - to display most important request info
        - `Desharp.Panels.Session` - to display basic session configuration and values
        - `Desharp.Panels.Routing` - to display matched MVC routes (still in TODO state)
    -->
    <add key=\"Desharp:Panels\" value=\"Desharp.Panels.SystemInfo,Desharp.Panels.Session\" />
    
    <!--
      Absolute or relative path from application root directory.
      - Not required, recomanded.
      - Relative path from app root has to start with \'~/\' like: \'~/Path/To/Logs\'.
      - If not configured, all log files are written into application root directory.
    -->
    <add key=\"Desharp:Directory\" value=\"~/Logs\" />
    
    <!-- 
      Always render source location from where dump has been called by `Debug.Dump()`.
      - Not required, recomanded.
      - Possible values: `1`, `0`, `true`, `false`.
      - If not configured, no dumps source locations are rendered in dump ouputs.
    -->

下载源码

通过命令行克隆项目:

git clone https://github.com/debug-sharp/desharp.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 desharp https://www.zuozi.net/33514.html

fszmq
上一篇: fszmq
Script Help
下一篇: Script Help
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务