前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 生成事件log

C# 生成事件log

作者头像
zls365
发布2020-08-19 11:04:13
4990
发布2020-08-19 11:04:13
举报
文章被收录于专栏:CSharp编程大全
代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace WindowsFormsApp31
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
        }
        private string str = "";
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread th1 = new Thread(test1);
            th1.Start();
            Thread th2 = new Thread(test2);
            th2.Start();
            timer1.Enabled = true;
        }
        public void test1()
        {
            try
            {
                if (!Directory.Exists(@"d:\test"))
                    Directory.CreateDirectory(@"d:\test");
                if (!File.Exists(@"d:\test\1.txt"))
                    File.Create(@"d:\test\1.txt").Close();
                StreamWriter sw = new StreamWriter(@"d:\test\1.txt", true);
                for (int i = 0; i < 100; i++)
                {
                    sw.WriteLine(i.ToString());
                }
                sw.Close();
                int k = 0;
                k = k / 0;//引发异常,写入事件log
            }
            catch(Exception ex)
            {
                StreamWriter sw = new StreamWriter(@"d:\test\1.txt", true);
                sw.WriteLine(ex.ToString());
                sw.Close();
            }
        }
        public void test2()
        {
            try
            {
                if (!Directory.Exists(@"d:\test"))
                    Directory.CreateDirectory(@"d:\test");
                if (!File.Exists(@"d:\test\2.txt"))
                    File.Create(@"d:\test\2.txt").Close();
                StreamWriter sw = new StreamWriter(@"d:\test\2.txt", true);
                for (int i = 0; i < 100; i++)
                {
                    sw.WriteLine(i.ToString());
                }
                sw.Close();
            }
            catch(Exception ex)
            {
                StreamWriter sw = new StreamWriter(@"d:\test\2.txt", true);
                sw.WriteLine(ex.ToString());
                sw.Close();
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            str += "1";
            richTextBox1.Text = str;
        }
    }
}

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

System.DivideByZeroException: 尝试除以零。

在 WindowsFormsApp31.Form1.test1() 位置 C:\Users\lin\source\repos\WindowsFormsApp31\WindowsFormsApp31\Form1.cs:行号 58

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-10-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CSharp编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档