C#窗体应用中打开控制输出内容

news/2024/9/24 1:18:06

窗体程序中打开控制台输出内容

namespace WinForms中打开控制台
{public partial class Form1 : Form{/////////////以下控制台调用相关代码///////////////////////////[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)][return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]static extern bool AllocConsole();[System.Runtime.InteropServices.DllImport("Kernel32")]public static extern void FreeConsole();/////////////以上控制台调用相关代码///////////////////////////int i = 0;public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){i++;AllocConsole();   //开启控制台Console.WriteLine("这是个测试"+i);}}
}

Nlog方式打印日志

参照 https://www.cnblogs.com/Can-daydayup/p/11182958.html

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><targets><!--此部分中的所有目标将自动异步--><target name="asyncFile" xsi:type="AsyncWrapper"><!--项目日志保存文件路径说明fileName="${basedir}/保存目录,以年月日的格式创建/${shortdate}/${记录器名称}-${单级记录}-${shortdate}.txt"--><target name="log_file" xsi:type="File"fileName="${basedir}/ProjectLogs/${shortdate}/${logger}-${level}-${shortdate}.txt"layout="${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}"archiveFileName="${basedir}/archives/${logger}-${level}-${shortdate}-{#####}.txt"archiveAboveSize="102400"archiveNumbering="Sequence"concurrentWrites="true"keepFileOpen="false" /></target><!--使用可自定义的着色将日志消息写入控制台--><target name="colorConsole" xsi:type="ColoredConsole" layout="[${date:format=HH\:mm\:ss}]:${message} ${exception:format=message}" /></targets><!--规则配置,final - 最终规则匹配后不处理任何规则--><rules><logger name="Microsoft.*" minlevel="Info" writeTo="" final="true" /><logger name="*" minlevel="Info" writeTo="asyncFile" /><logger name="*" minlevel="Warn" writeTo="colorConsole" /></rules>
</nlog>

配置配置文件,放在debug目录下
需打印日志的地方添加如下代码

            //声明对象var logger = NLog.LogManager.GetCurrentClassLogger();// 记录信息级别消息logger.Info("Greetings from NLog World!"+i);

日志如下
img

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/60308.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

推荐3款卓越的 .NET 开源搜索组件库

前言 最近有不少同学提问;.NET有哪些开源的搜索组件库可以推荐的吗?,今天大姚给大家推荐3款卓越的 .NET 开源搜索组件库,希望可以帮助到有需要的同学。 Elasticsearch .NET Elasticsearch 的 .NET 客户端为 Elasticsearch API 提供强类型请求和响应。它将协议处理委托给 El…

ATTCK红队评估(红日靶场5)

‍ 靶机介绍此次靶场虚拟机共用两个,一个外网一个内网,用来练习红队相关内容和方向,主要包括常规信息收集、Web攻防、代码审计、漏洞利用、内网渗透以及域渗透等相关内容学习,此靶场主要用来学习,请大家遵守网络网络安全法。‍‍注意:Win7双网卡模拟内外网‍ 环境搭建使用…

WordPress加载流程的解读分析

index.php ```php <?php /** 这个文件只用来加载 /wp-blog-header.php** @package WordPindex.php ```php <?php /* * 这个文件只用来加载 /wp-blog-header.php * * @package WordPress / /* * 声明一个全局变量,用来判断是否加载主题 * * @var bool / define(WP_USE_…

中秋节快乐简单html页面

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>中秋快乐</title> <style>@fo…

专业角度深入讲解:大模型备案(生成式人工智能)

一、什么是大模型备案? 大模型备案是指大模型产品在向公众开放及商用之前,经过国家互联网信息办公室(简称“网信办”)等监管部门的备案审批过程。 是为加强生成式人工智能服务的合规管理,通过备案制度,促进人工智能技术的健康发展,建立起一个既安全又可靠的人工智能服务…

PbootCMS怎么调用网站的留言数和文章总数

在 PBootCMS 中,可以使用 pboot:sql 标签来自定义任意查询语句并循环输出。下面详细介绍如何使用此标签来调用网站的留言数和文章总数。 1. 调用网站的留言数 示例代码html{pboot:sql sql="select count(id) as total from ay_message"}留言合计:[sql:total]条 {/p…