分享

C# 生成事件log

 ontheroad96j47 2021-11-08

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

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多